The document is generated by co-pilot with GPT-4o
-
/fake-async
Endpoint-
Behavior: This endpoint is marked as async, but it directly calls the synchronous function
services.heavy_computation_work()
. Sinceheavy_computation_work()
is synchronous and usestime.sleep
, it blocks the event loop while it runs. -
Why It Blocks: Even though the endpoint is
async
, the synchronous function (heavy_computation_work
) runs in the main event loop, preventing other tasks from being processed until it completes. -
Impact: This endpoint will block other requests (both async and sync) because the event loop is occupied by the blocking synchronous function.
-
-
/real-async
Endpoint-
Behavior: This endpoint is marked as
async
and calls the asynchronous functionservices.heavy_computation_work_async()
usingawait
. Theheavy_computation_work_async()
function usesawait asyncio.sleep
, which is non-blocking. -
Why It Does Not Block: the
await
keyword allows the event loop to continue processing other tasks while waiting for theheavy_computation_work_async()
function to complete. This ensures that the event loop remains free to handle other requests. -
Impact: This endpoint will not block other requests because it uses non-blocking asynchronous code.
-
-
/sync
Endpoint-
Behavior: This endpoint is synchronous and calls the synchronous function
services.heavy_computation_work()
. However, FastAPI automatically runs synchronous endpoints in a thread pool executor. -
Why It Does Not Block: FastAPI offloads the execution of synchronous endpoints to a separate thread in the thread pool. This means the synchronous function runs in a background thread, leaving the main event loop free to handle other requests.
-
Impact: This endpoint will not block other requests because it runs in a separate thread.
-