[BUG] BackgroundTasks not working properly
See original GitHub issueDescribe the bug
The request remains open/active until the background task finishes running, making the purpose of BackgroundTasks kind of useless. On the snippet below the request takes about 10 seconds to complete. I followed the documentation guide but it didnt seem to work.
To Reproduce
async def background_task(text: str, seconds: int):
import time
time.sleep(seconds)
print(text)
@app.get("/run", status_code=status.HTTP_200_OK)
async def test(background_tasks: BackgroundTasks, text: str = "Test", secs: int = 10):
background_tasks.add_task(background_task, text, secs)
return {"text": text, "secs": secs}
Screenshot
Expected behavior
The request to finish right away and the task to run in background.
Environment
- OS: Linux Manjaro 19
- FastAPI Version 0.54.1
- Python version 3.8.2
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:17 (6 by maintainers)
Top Results From Across the Web
Backgroundworker in fastapi is not working properly
Everything seems working but if I warp the background worker function in another function and call it then it's not working. Why does...
Read more >backgroundTask in SwiftUI cannot compile - Apple Developer
It's not a problem cause by modifier's order. In fact, If I only have one line of code inside WindowGroup, it can compile...
Read more >Background Tasks - FastAPI
First, import BackgroundTasks and define a parameter in your path operation function with a type declaration of BackgroundTasks :.
Read more >Solve the drawbacks with FastAPI BackgroundTasks - Johachi
If you have an error that is raised to the global error handler, then the background task won't be triggered. If this is...
Read more >Troubleshooting Background (async) Processing Issues
Troubleshooting Background (async) Processing Issues. About; Requirements; Logging; Admin Ajax request fails; cURL error 6; cURL error 7 ...
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
I’m not 100% sure about this but here’s a few guesses:
Have you tried to run it with
await asyncio.sleep(seconds)
instead of thetime.sleep
function?time.sleep
is synchronous as far as I know and since background tasks that are defined as “async” just use the event loop as their “background engine”, using a synchronous method in that background task will still globally block the process.Alternatively, you can try removing the “async” from
def background_task
. In that case the task should run in a thread pool instead which would then also not block.@soares7vinicius just fyi this: https://github.com/encode/starlette/issues/436 explains your problem es well. The tldr: