An open API service indexing awesome lists of open source software.

https://github.com/newtypegeek/fastapi-async-trap

To understand how sync and async work in FastAPI
https://github.com/newtypegeek/fastapi-async-trap

async fastapi sync

Last synced: 15 days ago
JSON representation

To understand how sync and async work in FastAPI

Awesome Lists containing this project

README

          

# fastapi-async-trap

The document is generated by co-pilot with GPT-4o

1. `/fake-async` Endpoint
- **Behavior**: This endpoint is marked as async, but it directly calls the synchronous function `services.heavy_computation_work()`. Since `heavy_computation_work()` is synchronous and uses `time.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.

2. `/real-async` Endpoint
- **Behavior**: This endpoint is marked as `async` and calls the asynchronous function `services.heavy_computation_work_async()` using `await`. The `heavy_computation_work_async()` function uses `await 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 the `heavy_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.

3. `/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.