Skip to main content

modern-di-celery

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

Modern-DI integration for Celery 5.x.

Full guide: Celery integration docs

Usage example: examples/

Installation

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

Usage

setup_di stores the container on app.conf and registers worker_process_init/worker_process_shutdown handlers that open/close it once per worker process. @inject builds a Scope.REQUEST child container per task call and resolves the FromDI-marked parameters from it; alternatively set task_cls=DITask to inject every task without a per-task decorator.

import typing

from celery import Celery
from modern_di import Container, Group, Scope, providers
from modern_di_celery import FromDI, inject, setup_di


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)


app = Celery("myapp", broker="redis://localhost")
container = Container(groups=[AppGroup])
setup_di(app, container)
container.validate()  # optional fail-fast; must come after setup_di registers its providers


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

The worker_process_init/worker_process_shutdown signals fire only when a real celery worker process starts and stops, so a script or test that runs tasks eagerly (task_always_eager = True) must drive the container lifecycle itself — send those signals or open/close the container directly. Celery tasks are sync, so REQUEST-scoped finalizers run via close_sync(); there is no framework request/message object, so no context provider is registered.

API

Symbol Description
setup_di(app, container) Stores the APP-scope container on app.conf and opens/closes it on worker_process_init/worker_process_shutdown. Returns the container
FromDI(dependency) Inert marker for Annotated[T, FromDI(...)] in task signatures; accepts a provider instance or a type
inject(task) Decorator that builds a Scope.REQUEST child per call, resolves the FromDI-annotated parameters, and closes the child with close_sync() afterwards
DITask Task subclass that applies @inject to a task's run automatically; pass task_cls=DITask to Celery(...) or base=DITask to @app.task(...)
fetch_di_container(app) Returns the APP-scope container registered with the Celery app

📦 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_celery-3.0.2.tar.gz (4.9 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_celery-3.0.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file modern_di_celery-3.0.2.tar.gz.

File metadata

  • Download URL: modern_di_celery-3.0.2.tar.gz
  • Upload date:
  • Size: 4.9 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_celery-3.0.2.tar.gz
Algorithm Hash digest
SHA256 384b297a10a651b5eb9771b825135f4096a94cadf58d9235c8d6eeadd2f94e1f
MD5 b601895300664b53c524cc3d3792dd3b
BLAKE2b-256 a4b403ea3357218e29a35c4686f83a6823ef57947b248411e52e48ca6ef69684

See more details on using hashes here.

File details

Details for the file modern_di_celery-3.0.2-py3-none-any.whl.

File metadata

  • Download URL: modern_di_celery-3.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.5 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_celery-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b846d78ec57dc8d9c2c05e028726726a9411fada0810731d3260cbfde97f355c
MD5 ab900e79920d5431bdb36be2e236b359
BLAKE2b-256 0ee8c3150e311c3bec154d2d291ef161e6fe713cf2db0542deb7c1957bd68a5a

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

3.0.2 This release

2 files

3.0.1

2 files

3.0.0

2 files

2.1.0

2 files

2.0.1

2 files

2.0.0

2 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