Python Forum
Starlette: accumulated tasks. Why? - Printable Version

+- Python Forum (
+-- Forum: Python Coding (
+--- Forum: Networking (
+--- Thread: Starlette: accumulated tasks. Why? (/thread-34195.html)

Starlette: accumulated tasks. Why? - lifemaker - Jul-06-2021

Hi everyone!
I have test project:

    from starlette.applications import Starlette
    from starlette.responses import JSONResponse
    from starlette.routing import Route
    from starlette.requests import Request
    from starlette.background import BackgroundTasks, BackgroundTask
    def parse_json(json_data):
    async def testbground(request: Request):
            json_data = await request.json()
        except JSONDecodeError:
            raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail='cannot_parse_request_body')
            return JSONResponse({"status": "Error"}, status_code=500)
        if 'value' in json_data.keys():
            tasks.add_task(parse_json, json_data)
            raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail='wrong_json_format')
        return JSONResponse({"status": "OK"}, background=tasks, status_code=200)
    tasks = BackgroundTasks()
    app = Starlette(debug=True, routes=[
        Route('/testbground', endpoint=testbground, methods=['POST']),
And have for tests:

    import requests
    url = 'http://localhost:8000/testbground'
    for i in range(10):
        data = {'value': i}
            r =, json=data, timeout=5)
        except Exception as e:

0 0 1 0 1 2 0 1 2 3 ... 0 1 2 3 4 5 6 7 8 9

But I was expecting another result.

0 1 2 ... 9
What is wrong? How can i add tasks to pull right?

RE: Starlette: accumulated tasks. Why? - DeaD_EyE - Jul-06-2021

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)
    return JSONResponse({"status": "OK"}, background=task, status_code=200)
INFO: Application startup complete. INFO: Uvicorn running on (Press CTRL+C to quit) INFO: - "POST /testbground HTTP/1.1" 200 OK 5 INFO: - "POST /testbground HTTP/1.1" 200 OK 6 INFO: - "POST /testbground HTTP/1.1" 200 OK 0 INFO: - "POST /testbground HTTP/1.1" 200 OK 1 INFO: - "POST /testbground HTTP/1.1" 200 OK 2 INFO: - "POST /testbground HTTP/1.1" 200 OK 3 INFO: - "POST /testbground HTTP/1.1" 200 OK 4
The implementation of these classes are simple:
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.

RE: Starlette: accumulated tasks. Why? - lifemaker - Jul-07-2021

(Jul-06-2021, 01:56 PM)DeaD_EyE Wrote: The BackgroundTasks are not deleted when the tasks has been finished, so it's executing all time all existing tasks.
Oh! This explains everything. Thanks so much!