[BUG] async BackgroundTasks run in the foreground
See original GitHub issueDescribe the bug
async BackgroundTasks
are not running as background tasks. My response blocks until the task finishes.
To Reproduce Steps to reproduce the behavior:
- Create a file
run.py
with contents
from fastapi import FastAPI, BackgroundTasks
import time
import asyncio
app = FastAPI()
async def sleep_for_10_seconds():
start = time.time()
while True:
now = time.time()
if now - start > 10:
break
asyncio.sleep(1)
print(time.time(), 'slept for 10 seconds')
@app.get("/")
async def read_root(background_tasks: BackgroundTasks):
print(time.time(), 'read_root')
background_tasks.add_task(sleep_for_10_seconds)
return {"Hello": "World"}
- Run with
uvicorn run:app --reload
- Run
curl http://localhost:8000
Expected behavior
{"Hello": "World"}
returns immediately. Instead it takes 10 seconds.
Environment:
- macOS
fastapi==0.27.0
uvicorn==0.8.4
- Python 3.6.6 :: Anaconda, Inc.
Additional context
I originally thought this problem was confined to asyncio.ensure_future()
in #403, but I’m getting the same behavior with fastapi.BackgroundTasks
.
sync tasks return immediately (i.e. don’t define with async).
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Run async tasks using Background T… - Apple Developer
I've got an iOS app that performs a series of operations when initialized and when a refresh is performed. In short, that app...
Read more >Background task and Foreground process crash with error ...
When I try to trigger the background task from Lifecycle events, both the foreground and the background process crash with error code 1....
Read more >Run async function on background thread? - Stack Overflow
Imagine someone writing await Task.Delay(-1) , and then be surprised that the next line of code is never reached. This person needs urgently ......
Read more >Asynchronous Background Processing using .NET Tasks
Creating an asynchronous background processing handler that leverages the .NET Task framework to replace the old BackgroundWorker.
Read more >Background Tasks - FastAPI
Create a function to be run as the background task. It is just a standard function that can receive parameters. It can be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@euri10 @ebanner I think I found the problem:
After making this change, it seems to behave properly.
I realised if you replaced asyncio.sleep with time.sleep it blocks the request anyone has a reason why async background tasks does not run in separate thread and how to potentially solve this problem