asgi-background
Background tasks for any ASGI framework.
Example (Starlette)
from asgi_background import BackgroundTaskMiddleware, BackgroundTasks
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Route
async def task(num: int) -> None:
await anyio.sleep(1)
print(num)
async def endpoint(request: Request) -> Response:
tasks = BackgroundTasks(request.scope)
await tasks.add_task(task, 1)
return Response()
app = Starlette(
routes=[Route("/", endpoint)],
middleware=[Middleware(BackgroundTaskMiddleware)]
)
Execution
Unlike Starlette, we do not execute background tasks within the ASGI request/response cycle.
Instead we schedule them in a TaskGroup that is bound to the application's lifespan.
The 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.
Just 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.
For 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.
Metadata
Release files for asgi-background 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| asgi_background-0.2.2.tar.gz | 4.9 kB | Without provenance | Details for asgi_background-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| asgi_background-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for asgi_background-0.2.2-py3-none-any.whl |
Total release size: 10.2 kB
Release files / asgi_background-0.2.2.tar.gz
| Download URL | asgi_background-0.2.2.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5046aa0c54690ac980bfe8049b9d3288b352c29d35720397f83be7e825c0f598
|
|
BLAKE2b-256 checksum How to use checksums |
4fa6972a433cf7329307e23db2b5f5b6b7ae82a18152e0e83de5fd56857bc240
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.10.3 Darwin/21.6.0
|
Release files / asgi_background-0.2.2-py3-none-any.whl
| Download URL | asgi_background-0.2.2-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3d928d74b313fd45ddeeb77272fd7f6edb239cbeee5d0359c4684dca4990ca23
|
|
BLAKE2b-256 checksum How to use checksums |
c6c8302c68d5b00a15eaa6599634d742df392a834af3337c62d0cb0f7440adb2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.10.3 Darwin/21.6.0
|
Security for asgi-background 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.