Taskiq integration with litestar
Project description
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.
State
fromlitestar.datastructures
;Litestar
fromlitestar
.
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.
import taskiq_litestar
from taskiq import InMemoryBroker
broker = InMemoryBroker()
app = FastAPI()
taskiq_fastapi.init(broker, "test_script:app")
taskiq_fastapi.populate_dependency_context(broker, app)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file taskiq_litestar-0.1.1.tar.gz
.
File metadata
- Download URL: taskiq_litestar-0.1.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-1012-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bd8d5e615d0a8bcf08e382d4f849ea65b8f673b8611f91de871a08a4160c865 |
|
MD5 | 3bf28ff99f9bc6e7fc4cd1ded3f952be |
|
BLAKE2b-256 | d8b4a0858e142b041d88ea3ea207dcd5953d03e567f4c9db090e4bdb69f914db |
File details
Details for the file taskiq_litestar-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: taskiq_litestar-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-1012-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b77f775c1886fd0f1dfb3f4ec938706116a94cb7f26e12d9e1fee7d960a16925 |
|
MD5 | 3d21f5e627dcd3e39883f221ed0161a6 |
|
BLAKE2b-256 | 069a1ca79e5200da13ed5a62b9578cca7382e6c270a028b53f4287a01be4225b |