taskiq-dependencies is a Python library providing a FastAPI-like dependency injection system. It allows for defining dependencies as functions and automatically resolving them for other callables. As a core component of the Taskiq asynchronous task queue ecosystem, it is actively maintained with frequent updates, currently at version 1.5.7.
pip install taskiq-dependenciesVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define and use dependencies with `taskiq-dependencies`. It covers both synchronous and asynchronous dependencies, showing how to use `Depends` to declare requirements and `inject_dependencies` to manually resolve and call the target function.
Always wrap your dependent function call with `inject_dependencies()`.
Be mindful of the performance implications if dependencies are expensive to compute. For specific lifecycle management within `taskiq` tasks, consider how `taskiq`'s own integration (e.g., `TaskiqDepends`) might layer on top.
For `taskiq` tasks, prefer `from taskiq import TaskiqDepends as Depends` to ensure full compatibility with the task queue's features and lifecycle management.
pip install taskiq-dependencies
Ensure that the `Depends()` argument correctly points to a defined dependency function and that all nested dependencies are properly specified and resolvable, including correct type hints.
Pass the dependency function reference to `Depends()`, not its call result: `db=Depends(get_db)`.
First, create an instance of `DependencyInjector` (e.g., `injector = DependencyInjector()`), then use its `@collector` decorator (e.g., `@injector.collector`).