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.

[QUESTION] Long running background tasks

See original GitHub issue

Hey everyone, I’m currently trying to implement an API endpoint using FastAPI which starts a long running background task using asyncio.create_task(startlongrunningtask()) and then without waiting for that task to finish, return a response to the client. The long running task starts up fine, but times out after e few seconds with the error message [CRITICAL] WORKER TIMEOUT and Booting worker with pid: 25

How can I prevent the worker from getting timed out?

Is it possible to create such long running tasks which continue even after sending a response to the client?

Thanks a lot and best regards

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

6reactions
dmontagucommented, Oct 11, 2019

@xiaodiannao I’m curious to hear if switching to def fixes it.


This isn’t really the intended use case for BackgroundTasks – I think BackgroundTasks is intended more for shorter, low-compute tasks like triggering an email send or similar.

For tasks involving multi-minute calls to subprocesses, I think long term you would benefit from putting in the effort to set up a celery worker and a task queue, and have the celery worker do the subprocess stuff. (I have a similar setup on one of my own projects, which runs multi-minute compute-intensive jobs.) That will decouple your server from the worker, and may help you avoid/preempt a lot of issues arising from long-running and/or compute-intensive jobs running alongside your server.

If you wanted to do this, the full stack fastapi postgres template has a celery worker; you should be able to adapt it to your use case.

1reaction
euri10commented, Oct 10, 2019

happy to help but you’d need to give more info on what the tasks does, how you’re running the app, what worker stops (uvicorn one or gunicorn)

the below snippet shows a long running background task in action, it just sleeps and doesn’t use asyncio.create_task like you describe though

Connected to pydev debugger (build 192.6817.19)
email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
2019-10-10 13:18:21,122 uvicorn      INFO     Started server process [29570]
2019-10-10 13:18:21,122 uvicorn      INFO     Waiting for application startup.
2019-10-10 13:18:21,124 uvicorn      INFO     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
2019-10-10 13:18:29,706 uvicorn      INFO     ('127.0.0.1', 46716) - "POST /backgroundasync HTTP/1.1" 200
2019-10-10 13:18:29,706 __main__     DEBUG    sleeping 30s
2019-10-10 13:18:59,707 __main__     DEBUG    slept 30s
import asyncio
import logging
from typing import Dict

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from starlette.background import BackgroundTasks

logging.basicConfig(format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
logger: logging.Logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


app = FastAPI()


class Item(BaseModel):
    amount: int


async def background_async(amount: int) -> None:
    logger.debug(f"sleeping {amount}s")
    await asyncio.sleep(amount)
    logger.debug(f"slept {amount}s")


@app.post("/backgroundasync")
async def sleepingtheback(
    item: Item, background_tasks: BackgroundTasks
) -> Dict[str, str]:
    background_tasks.add_task(background_async, item.amount)
    return {"message": f"sleeping {item.amount} in the back"}


if __name__ == "__main__":
    uvicorn.run(app=app)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Run a Long-Running Background Task in Python
In this tutorial you will discover how to execute long-running tasks in the background in Python. Let's get started. Table of Contents.
Read more >
python - Long-running compute-intensive tasks in APIs
So, I thought to use Celery to offload this work to a background worker on a task queue. This works well; the API...
Read more >
Background Tasks - FastAPI
You can define background tasks to be run after returning a response. This is useful for operations that need to happen after a...
Read more >
c# - How to implement parallel long running background tasks ...
So I have two questions: Is there a way to make these two run in parallel? Are there any other alternatives to run...
Read more >
How to avoid long-running background jobs being executed ...
Question How to avoid long-running background jobs being executed on main thread · 1) Optimize the large job (Burst helps a lot) ·...
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