Python Forum
Starlette: accumulated tasks. Why?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Starlette: accumulated tasks. Why?
#2
The BackgroundTasks are not deleted when the tasks has been finished, so it's executing all time all existing tasks.

Remove line 32: tasks = BackgroundTasks()

In line 23 replace:
task = BackgroundTask(parse_json, json_data)
# don't forget to import BackgroundTask
# don't use BackgroundTasks

And change
    return JSONResponse({"status": "OK"}, background=tasks, status_code=200)
to
    return JSONResponse({"status": "OK"}, background=task, status_code=200)
Output:
INFO: Application startup complete. INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: 127.0.0.1:61469 - "POST /testbground HTTP/1.1" 200 OK 5 INFO: 127.0.0.1:61471 - "POST /testbground HTTP/1.1" 200 OK 6 INFO: 127.0.0.1:61474 - "POST /testbground HTTP/1.1" 200 OK 0 INFO: 127.0.0.1:61476 - "POST /testbground HTTP/1.1" 200 OK 1 INFO: 127.0.0.1:61478 - "POST /testbground HTTP/1.1" 200 OK 2 INFO: 127.0.0.1:61480 - "POST /testbground HTTP/1.1" 200 OK 3 INFO: 127.0.0.1:61482 - "POST /testbground HTTP/1.1" 200 OK 4
The implementation of these classes are simple: https://github.com/encode/starlette/blob...ound.py#L7
The Response class takes the background and run it as a coroutine or as a thread if it's not a coroutine.

Why the error happens, is also self-explanatory. In BackgroundTasks class is no automagically method to remove a task.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Starlette: accumulated tasks. Why? - by lifemaker - Jul-06-2021, 09:26 AM
RE: Starlette: accumulated tasks. Why? - by DeaD_EyE - Jul-06-2021, 01:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to manage a dynamic list of tasks with asyncio tperrot 0 2,889 Aug-06-2021, 09:07 AM
Last Post: tperrot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020