Skip to main content

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

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

taskiq_litestar-0.1.0.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

taskiq_litestar-0.1.0-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page