Taskiq Litestar
This project adds integration with Litestar to TaskIQ.
Mainly this project focuses on running starup and shutdown events declared in your litestar app on worker nodes. This will allow you to access application's state and data from within your tasks.
Also we add a few dependencies that you can depend on in your tasks.
Statefromlitestar.datastructures;Litestarfromlitestar.
Installation
pip install taskiq-litestar
Usage
Here we have a script called test_script.py so the listestar app can be found at test_script:app. We use strings to resolve application to bypass circular imports.
In the declared task I depend on a state.
from contextlib import asynccontextmanager
from litestar import Litestar, get
from litestar.datastructures import State
from taskiq import TaskiqDepends
from taskiq_redis import ListQueueBroker
import taskiq_litestar
broker = ListQueueBroker("redis://localhost:6379/0")
taskiq_litestar.init(
broker,
"test_script:app",
)
@asynccontextmanager
async def app_lifespan(app: Litestar) -> None:
"""Lifespan generator."""
if not broker.is_worker_process:
await broker.startup()
app.state.value = "abc123"
yield
if not broker.is_worker_process:
await broker.shutdown()
@broker.task()
async def my_task(state: State = TaskiqDepends()) -> None:
"""My task."""
print("a", state.dict()) # noqa: T201
@get("/")
async def index() -> str:
"""Index get handler."""
await my_task.kiq()
return "Task sent"
app = Litestar([index], lifespan=[app_lifespan])
Manually update dependency context
When using InMemoryBroker you can manually update the dependency context. This might come handy when setting up tests.
from litestar import Litestar
import taskiq_litestar
from taskiq import InMemoryBroker
broker = InMemoryBroker()
app = Litestar()
taskiq_litestar.init(broker, "test_script:app")
taskiq_litestar.populate_dependency_context(broker, app)
Metadata
Release files for taskiq-litestar 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| taskiq_litestar-0.2.0.tar.gz | 4.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| taskiq_litestar-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.2 kB
Release files / taskiq_litestar-0.2.0.tar.gz
| Download URL | taskiq_litestar-0.2.0.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fd4c810faf41510650432cba70755ff33b803f869dd7c8a4b043785bea668546
|
|
BLAKE2b-256 checksum How to use checksums |
6670648a96ce0267308afd9760cb2099356eef73cd350dc69bca82e422365266
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / taskiq_litestar-0.2.0-py3-none-any.whl
| Download URL | taskiq_litestar-0.2.0-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b47dbdc455494e83476f1d5c43d366e32ba7c92e6afdd1fc721db14bcd7c4372
|
|
BLAKE2b-256 checksum How to use checksums |
d84698cbc97a9a8fabff658a1c512a4c54c69570a90b9db48d7d80561b095674
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|