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.

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)

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.2.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

taskiq_litestar-0.2.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file taskiq_litestar-0.2.0.tar.gz.

File metadata

  • Download URL: taskiq_litestar-0.2.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for taskiq_litestar-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fd4c810faf41510650432cba70755ff33b803f869dd7c8a4b043785bea668546
MD5 2e47239ca1b85f562b2077200173ec08
BLAKE2b-256 6670648a96ce0267308afd9760cb2099356eef73cd350dc69bca82e422365266

See more details on using hashes here.

File details

Details for the file taskiq_litestar-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: taskiq_litestar-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for taskiq_litestar-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b47dbdc455494e83476f1d5c43d366e32ba7c92e6afdd1fc721db14bcd7c4372
MD5 d7d7be27a869377d055c13d5fdc921f9
BLAKE2b-256 d84698cbc97a9a8fabff658a1c512a4c54c69570a90b9db48d7d80561b095674

See more details on using hashes here.

Supported by

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