Skip to main content

modern-di-taskiq

PyPI version Supported Python versions Downloads Coverage CI License GitHub stars uv Ruff ty

Modern-DI integration for taskiq.

Full guide: taskiq integration docs

Installation

uv add modern-di-taskiq      # or: pip install modern-di-taskiq

Usage

setup_di stores the container on broker.state and registers WORKER_STARTUP/WORKER_SHUTDOWN handlers that open/close it, and builds a Scope.REQUEST child container for each task the worker executes. FromDI resolves a provider (or type) into a task parameter — no per-task decorator is needed.

import typing

from modern_di import Container, Group, Scope, providers
from modern_di_taskiq import FromDI, setup_di
from taskiq import InMemoryBroker


class Settings:
    def __init__(self) -> None:
        self.greeting = "hello"


class Greeter:
    def __init__(self, settings: Settings) -> None:   # auto-injected by type
        self._settings = settings

    def greet(self, name: str) -> str:
        return f"{self._settings.greeting}, {name}"


class AppGroup(Group):
    settings = providers.Factory(Settings, scope=Scope.APP, cache=True)
    greeter = providers.Factory(Greeter, scope=Scope.REQUEST)


broker = InMemoryBroker()
setup_di(broker, Container(groups=[AppGroup], validate=True))


@broker.task
async def greet(
    name: str,
    greeter: typing.Annotated[Greeter, FromDI(Greeter)],   # resolve by type
) -> str:
    return greeter.greet(name)

The WORKER_STARTUP/WORKER_SHUTDOWN events fire when the broker's worker process starts and stops, so a script that calls tasks directly (like InMemoryBroker in a test) must drive the container lifecycle itself — e.g. async with broker: .... The per-task Scope.REQUEST child is torn down asynchronously (close_async()), so async REQUEST-scoped finalizers run correctly while factories build synchronously. taskiq.TaskiqMessage is resolvable within DI via the pre-built taskiq_message_provider context provider.

API

Symbol Description
setup_di(broker, container) Stores the APP-scope container on broker.state, opens/closes it on worker startup/shutdown, and builds a Scope.REQUEST child container per task. Returns the container
FromDI(dependency) Inert marker for Annotated[T, FromDI(...)] in task signatures; accepts a provider instance or a type
fetch_di_container(broker) Returns the APP-scope container registered with the taskiq broker
taskiq_message_provider ContextProvider for the current taskiq.TaskiqMessage (REQUEST scope)

📦 PyPI

📝 License

Part of modern-python

Built on modern-di, a dependency-injection framework with IoC container and scopes.

Browse the full list of templates and libraries in modern-python — see the org profile for the categorized index.

Download files

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

Source Distribution

modern_di_taskiq-2.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

modern_di_taskiq-2.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file modern_di_taskiq-2.1.0.tar.gz.

File metadata

  • Download URL: modern_di_taskiq-2.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"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 modern_di_taskiq-2.1.0.tar.gz
Algorithm Hash digest
SHA256 d209d6679a8487e1166c645a28f49ea6502bf9a968315c118e68a1c1867219b6
MD5 dc9eda21232fc1ee3e063573bb8d7809
BLAKE2b-256 3e4aae6b460ec9c4135ea1aaa8495fd3c4d986854b21cfec453176f9221cbd76

See more details on using hashes here.

File details

Details for the file modern_di_taskiq-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: modern_di_taskiq-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"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 modern_di_taskiq-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4723ca729126a4ce9d2e143ba2af4e3cdc138ec12c3d6b451118841ae7790b5a
MD5 c4ba800eb2cae3dc6ed85f18c57f3036
BLAKE2b-256 d9e2c328bf32dbad3511febba94f79f9fe065e056f02d160f04301e3a2e4b6cd

See more details on using hashes here.

Release history Release notifications | RSS feed

3.0.1

2 files

3.0.0

2 files

This release

2.1.0 This release

2 files

2.0.0

2 files

Supported by

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