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
- Host: GitHub
- URL: https://github.com/newtypegeek/fastapi-async-trap
- Owner: newTypeGeek
- License: mit
- Created: 2025-03-19T14:09:38.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-03-19T15:30:06.000Z (about 1 year ago)
- Last Synced: 2025-08-20T10:37:33.789Z (10 months ago)
- Topics: async, fastapi, sync
- Language: Python
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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.