question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] BackgroundTasks not working properly

See original GitHub issue

Describe 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

Screenshot from 2020-04-22 17-16-17

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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
daviskirkcommented, Apr 22, 2020

I’m not 100% sure about this but here’s a few guesses:

  1. Have you tried to run it with await asyncio.sleep(seconds) instead of the time.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.

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

5reactions
daviskirkcommented, Apr 23, 2020

@soares7vinicius just fyi this: https://github.com/encode/starlette/issues/436 explains your problem es well. The tldr:

  • async function background task => runs in asyncio loop (everything that doesn’t use “await” will block)
  • normal function background task => runs in a threadpool (and will thus not block in this case)
Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found