taskiq¶
Though it is not required, you can use dishka-taskiq integration. It features:
automatic REQUEST scope management using middleware
passing
TaskiqMessageobject as a context data to providersinjection of dependencies into task handler function using decorator.
How to use¶
Import
from dishka.integrations.taskiq import (
FromDishka,
inject,
setup_dishka,
TaskiqProvider,
)
from dishka import make_async_container, Provider, provide, Scope
Create provider. You can use
taskiq.TaskiqMessageas a factory parameter to access on REQUEST-scope
class YourProvider(Provider):
@provide(scope=Scope.REQUEST)
def create_x(self, event: TaskiqMessage) -> X:
...
Mark those of your handlers parameters which are to be injected with
FromDishka[]and decorate them using@inject
@broker.task
@inject(patch_module=True)
async def start(
gateway: FromDishka[Gateway],
):
...
Warning
In version 1.5, the patch_module parameter was added to the inject decorator, which is responsible for overriding the __module__ attribute of the function that participates in the formation of task_name.
It is recommended to use the value patch_module=True, to correctly generate the default task_name according to the module in which the task handler was defined.
The default value is False, for backward compatibility with versions < 1.5. In future releases, the default value may be changed to True.
(optional) Use
TaskiqProvider()when creating container if you are going to usetaskiq.TaskiqMessagein providers
container = make_async_container(YourProvider(), TaskiqProvider())
Setup
dishkaintegration.
setup_dishka(container=container, broker=broker)