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
| ... | |
| async def upload_to_s3(): | |
| await do_async_work_to_prepare_for_the_upload() | |
| # assume the upload will run asynchronously for 5 seconds, such as | |
| # when using aioboto3 (unofficial async-compatible aws sdk) | |
| logger.info("Before sleep") | |
| await asyncio.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(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