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 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-3.0.1.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-3.0.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modern_di_taskiq-3.0.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.33 {"installer":{"name":"uv","version":"0.11.33","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-3.0.1.tar.gz
Algorithm Hash digest
SHA256 9bed40895ef4d9432ad4024d11ad3e299dd7daf4bd43633c12f95d20dd7b34d4
MD5 6e018aff35c5c7b86612fc6e2268ab45
BLAKE2b-256 b8a440c287f5217333e1d14667448b0882bbf9006c0e46bf00c4a4ec7900ad0d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modern_di_taskiq-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.33 {"installer":{"name":"uv","version":"0.11.33","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-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 711c9199e581a0536bb3e03c87b4c451cc573a075d86ad4c101bde3677dcddee
MD5 129dc854c0cc1635c1d0853996d20fcf
BLAKE2b-256 801c466ecf967a06805466c64d3ae69d0d6ec26488c9aeefbc9f541e4983c5ba

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

3.0.1 This release

2 files

3.0.0

2 files

2.1.0

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