Modern-DI integration for arq.
Full guide: arq integration docs
Usage example: examples/
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 builds a Scope.REQUEST child container per job. Decorate a task with @inject to resolve its FromDI-marked parameters from that per-job child — the child is open only while @inject-decorated task body/bodies are running, reference-counted so nested and concurrent (asyncio.gather) @inject calls over the same job share one open child and close it exactly once, guaranteeing teardown even if arq skips on_job_end.
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")
container = Container(groups=[AppGroup])
setup_di(WorkerSettings, container)
container.validate() # optional fail-fast; must come after setup_di registers its providers
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. Raises RuntimeError naming setup_di when a job reaches it without the modern-di hooks installed |
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.
Metadata
Release files for modern-di-arq 3.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| modern_di_arq-3.1.2.tar.gz | 6.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modern_di_arq-3.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.4 kB
Release files / modern_di_arq-3.1.2.tar.gz
| Download URL | modern_di_arq-3.1.2.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cf163753283b4e805c360410146aefc4e6dd82023e25c2c0ead5696212368b26
|
|
BLAKE2b-256 checksum How to use checksums |
0f4ab70d00bcdc8c97a9bef7cb5fd4ba383740b2615f0b0d3ac1eeda40327a24
|
| 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_arq-3.1.2-py3-none-any.whl
| Download URL | modern_di_arq-3.1.2-py3-none-any.whl |
|---|---|
| Size | 6.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1a9d7e5ba6c9a3ecdf3ba112db0472cad0a1d7a870f82fdfc725e6361f2cce1b
|
|
BLAKE2b-256 checksum How to use checksums |
43a2da9d83cf94d584f14bdddac98dca432da53189af668c6f3574c507f91e85
|
| 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}
|