Metadata-Version: 2.4
Name: taskiq-litestar
Version: 0.2.0
Summary: Taskiq integration with litestar
Keywords: taskiq,tasks,distributed,async,litestar
Author: Taskiq team
Author-email: Taskiq team <taskiq@no-reply.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Typing :: Typed
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Topic :: System :: Networking
Classifier: Development Status :: 3 - Alpha
Requires-Dist: litestar>=2.18.0
Requires-Dist: taskiq>=0.12.0
Requires-Dist: black>=25.11.0 ; extra == 'dev'
Requires-Dist: coverage>=7.12.0 ; extra == 'dev'
Requires-Dist: mypy>=1.19.0 ; extra == 'dev'
Requires-Dist: pre-commit>=4.5.0 ; extra == 'dev'
Requires-Dist: pytest>=9.0.1 ; extra == 'dev'
Requires-Dist: pytest-cov>=7.0.0 ; extra == 'dev'
Requires-Dist: ruff>=0.14.7 ; extra == 'dev'
Maintainer: Taskiq team
Maintainer-email: Taskiq team <taskiq@no-reply.com>
Requires-Python: >=3.10
Provides-Extra: dev
Description-Content-Type: text/markdown

# Taskiq Litestar

This project adds integration with [Litestar](https://litestar.dev/) to [TaskIQ](https://taskiq-python.github.io/).

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` from `litestar.datastructures`;
* `Litestar` from `litestar`.

# Installation

```bash
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.

```python
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.

```python
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)
```
