Skip to main content

modern-di-arq

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

Modern-DI integration for arq.

Full guide: arq integration docs

Installation

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

Usage

setup_di seeds the root container into arq's ctx dict and wires four of arq's lifecycle hooks: on_startup/on_shutdown open and close the root container, and on_job_start/on_job_end build and close a Scope.REQUEST child container around each job. Decorate a task with @inject to resolve its FromDI-marked parameters from that per-job child.

import typing

from arq.connections import RedisSettings
from modern_di import Container, Group, Scope, providers
from modern_di_arq 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)


@inject
async def greet(
    ctx: dict[str, typing.Any],       # arq passes its context dict as the first argument
    name: str,
    greeter: typing.Annotated[Greeter, FromDI(Greeter)],   # resolve by type
) -> str:
    return greeter.greet(name)


class WorkerSettings:
    functions = [greet]
    redis_settings = RedisSettings(host="localhost")


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

Run the worker as usual (arq mymodule.WorkerSettings) and enqueue jobs with only their real arguments — await pool.enqueue_job("greet", "world") — the FromDI parameters are resolved for you. A task must declare arq's ctx dict as its first parameter; injection is order-insensitive otherwise. arq's ctx is a plain dict (not a dedicated message type), so no context provider is registered — read job metadata from ctx, and fetch_di_container(ctx) returns the root container.

API

Symbol Description
setup_di(worker_settings, container) Seeds the root container into arq's ctx and wires root + per-job lifecycle onto on_startup/on_shutdown/on_job_start/on_job_end. Accepts a WorkerSettings class/object or a settings dict; composes with existing hooks; returns the container. Raises TypeError if called twice on the same worker_settings
FromDI(dependency) Inert marker for Annotated[T, FromDI(...)] in task signatures; accepts a provider instance or a type
inject(task) Decorator that resolves FromDI-annotated parameters from the per-job Scope.REQUEST child. Order-insensitive; passthrough for tasks with no FromDI; raises TypeError at decoration if the task also declares *args/**kwargs
fetch_di_container(ctx) Returns the root container from an arq ctx dict

📦 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_arq-3.0.0.tar.gz (5.5 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_arq-3.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file modern_di_arq-3.0.0.tar.gz.

File metadata

  • Download URL: modern_di_arq-3.0.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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_arq-3.0.0.tar.gz
Algorithm Hash digest
SHA256 2af5a61146dd1d4240ad105c193c8c97a050014af8074f0313ef6b9a61677365
MD5 89cab2a3dc0ad170a1ca634517fc7bb0
BLAKE2b-256 4cd4f560ad44ff8427da04b95f45e11dc62806ee2c9551fd3bfcb13a9c068b6e

See more details on using hashes here.

File details

Details for the file modern_di_arq-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: modern_di_arq-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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_arq-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c5290f9b0321cf3088bbb406fe461fcb50a41fd80ee61693f361bed17f581475
MD5 73c83098237464878e0d4e115ec7900d
BLAKE2b-256 5c22a20d48008d831b02a173e6adafc1885addcc269f17a7190ce4560f928377

See more details on using hashes here.

Release history Release notifications | RSS feed

3.1.1

2 files

3.1.0

2 files

This release

3.0.0 This release

2 files

2.1.0

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