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

Usage example: examples/

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()
container = Container(groups=[AppGroup])
setup_di(broker, container)
container.validate()  # optional fail-fast; must come after setup_di registers its providers


@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 root 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. Raises RuntimeError naming setup_di when a task reaches it without setup_di called
fetch_di_container(broker) Returns the root container attached to 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.

Metadata

Release files for modern-di-taskiq 3.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modern-di-taskiq 3.0.2
File Size Uploaded
modern_di_taskiq-3.0.2.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for modern-di-taskiq 3.0.2
File Interpreter ABI Platform
modern_di_taskiq-3.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / modern_di_taskiq-3.0.2.tar.gz

Download URL modern_di_taskiq-3.0.2.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
297ccdce22a4a8dafaf106bc316e525a6d265b16bb5110c60494344ed80ebd99
BLAKE2b-256 checksum
How to use checksums
9e87e93465638570d3b5f8839e0d6cf3b968d35ba75b8b2c8e01b3158690d144
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","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}

Release files / modern_di_taskiq-3.0.2-py3-none-any.whl

Download URL modern_di_taskiq-3.0.2-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72a148a6284ea8dabd998564320e9ee6f84766c952aa70495a4bb5b9279b3a7c
BLAKE2b-256 checksum
How to use checksums
3b2268329cc7f5af40a65f7edd858547ecf8d054c1e75b78b9de82767022da07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","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}

Release history Release notifications | RSS feed

4.0.0

2 release files

3.1.0

2 release files

This release

3.0.2 This release

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page