Skip to content

Instantly share code, notes, and snippets.

@CodeWithOz
Last active October 9, 2025 05:36
Show Gist options
  • Select an option

  • Save CodeWithOz/d31e788d4bec21eec47a3247e593163e to your computer and use it in GitHub Desktop.

Select an option

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.
...
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