Last active
October 9, 2025 05:36
-
-
Save CodeWithOz/d31e788d4bec21eec47a3247e593163e to your computer and use it in GitHub Desktop.
Code snippets showing different approaches to resolving blocking operations in a FastAPI server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| from fastapi.concurrency import run_in_threadpool | |
| ... | |
| def prepare_for_upload_synchronously(): | |
| pass | |
| def upload_to_s3(): | |
| prepare_for_upload_synchronously() | |
| # assume the upload will run synchronously for 5 seconds | |
| logger.info("Before sleep") | |
| sleep(5) | |
| logger.info("After sleep") | |
| @app.get("/process-files") | |
| async def process_files(background_tasks: BackgroundTasks): | |
| logger.info("Before process files") | |
| background_tasks.add_task(run_in_threadpool, upload_to_s3) | |
| logger.info("After process files") | |
| return {"ok": True} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment