InterviewHubPRO
Section Overview
Q45

Question 45 of 65

Part 4: Web Frameworks, APIs & FastAPI (41 - 55)

69%
Python & FastAPI•Part 4: Web Frameworks, APIs & FastAPI (41 - 55)

`async def` vs standard `def` in FastAPI endpoints:

In Simple Words:

  • async def: Runs directly on the main AsyncIO event loop thread. Use when calling await on non-blocking async libraries.
  • Standard def: FastAPI automatically offloads standard def routes to an internal worker thread pool, preventing blocking code from freezing the event loop!
python
# Use standard def for legacy synchronous blocking libraries:
import time

@app.get("/sync-task")
def sync_endpoint():
    time.sleep(1) # Safe: FastAPI offloads this to an external thread pool!
    return {"status": "completed"}