Back to Taskiq

Middlewares

docs/extending-taskiq/middleware.md

0.12.3842 B
Original Source

Middlewares

Middlewares are super helpful. You can inject some code before or after task's execution.

Middlewares must implement taskiq.abc.middleware.TaskiqMiddleware abstract class. Every method of a middleware can be either sync or async. Taskiq will execute it as you expect.

For example:

::: tabs

@tab sync

@code python

@tab async

@code python

:::

Also, middlewares always have reference to the current broker in self.broker field. If you want to kick a message during the execution of some middleware hooks, you may use self.broker to do so.

Taskiq-pipelines uses middlewares to call next tasks.