{"id":13598549,"url":"https://github.com/adriangb/asgi-background","last_synced_at":"2025-04-19T18:49:25.988Z","repository":{"id":45666689,"uuid":"509269533","full_name":"adriangb/asgi-background","owner":"adriangb","description":"Background tasks for ASGI web frameworks","archived":false,"fork":false,"pushed_at":"2022-11-03T20:35:29.000Z","size":61,"stargazers_count":18,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-15T01:39:59.119Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adriangb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-01T00:14:09.000Z","updated_at":"2024-11-28T21:37:42.000Z","dependencies_parsed_at":"2023-01-20T23:16:16.498Z","dependency_job_id":null,"html_url":"https://github.com/adriangb/asgi-background","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriangb%2Fasgi-background","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriangb%2Fasgi-background/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriangb%2Fasgi-background/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriangb%2Fasgi-background/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adriangb","download_url":"https://codeload.github.com/adriangb/asgi-background/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249239277,"owners_count":21235829,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T17:00:53.572Z","updated_at":"2025-04-16T12:30:51.364Z","avatar_url":"https://github.com/adriangb.png","language":"Python","funding_links":[],"categories":["Resources"],"sub_categories":["Experiments and examples"],"readme":"# asgi-background\n\nBackground tasks for any ASGI framework.\n\n## Example (Starlette)\n\n```python\nfrom asgi_background import BackgroundTaskMiddleware, BackgroundTasks\nfrom starlette.applications import Starlette\nfrom starlette.middleware import Middleware\nfrom starlette.requests import Request\nfrom starlette.responses import Response\nfrom starlette.routing import Route\n\n\nasync def task(num: int) -\u003e None:\n    await anyio.sleep(1)\n    print(num)\n\n\nasync def endpoint(request: Request) -\u003e Response:\n    tasks = BackgroundTasks(request.scope)\n    await tasks.add_task(task, 1)\n    return Response()\n\n\napp = Starlette(\n    routes=[Route(\"/\", endpoint)],\n    middleware=[Middleware(BackgroundTaskMiddleware)]\n)\n```\n\n## Execution\n\nUnlike Starlette, we do not execute background tasks within the ASGI request/response cycle.\nInstead we schedule them in a `TaskGroup` that is bound to the application's lifespan.\nThe only guarantee we make is that background tasks will not block (in the async sense, not the GIL sense) sending the response and that we will (try) to wait for them to finish when the application shuts down.\nJust like with Starlette's background tasks, you should only use these for short lived tasks, they are not a durable queuing mechanisms like Redis, Celery, etc.\nFor context, the default application shutdown grace period in Kubernetes is 30 seconds, so 30 seconds is probably about as long as you should allow your tasks to run.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadriangb%2Fasgi-background","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadriangb%2Fasgi-background","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadriangb%2Fasgi-background/lists"}