You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are two rules to make everything work as you expect:
@@ -43,7 +42,6 @@ from typing import Any
43
42
44
43
defget_redis_pool(request: Request) -> Any:
45
44
return request.app.state.redis_pool
46
-
47
45
```
48
46
49
47
To make it resolvable in taskiq, people should mark default fastapi dependencies (such as `Request` and `HTTPConnection`) with `TaskiqDepends`. Like this:
@@ -61,7 +59,6 @@ from taskiq import TaskiqDepends
Also you want to call startup of your brokers somewhere.
83
79
80
+
::: tabs
81
+
82
+
@tab Lifespan (Recommended)
83
+
84
+
```python
85
+
from contextlib import asynccontextmanager
86
+
from fastapi import FastAPI
87
+
from your_project.taskiq import broker
88
+
89
+
90
+
@asynccontextmanager
91
+
asyncdeflifespan(app: FastAPI):
92
+
# Startup
93
+
ifnot broker.is_worker_process:
94
+
await broker.startup()
95
+
yield
96
+
# Shutdown
97
+
ifnot broker.is_worker_process:
98
+
await broker.shutdown()
99
+
100
+
101
+
app = FastAPI(lifespan=lifespan)
102
+
```
103
+
104
+
@tab on_event (Deprecated)
105
+
84
106
```python
85
107
from fastapi import FastAPI
86
108
from your_project.taskiq import broker
@@ -89,7 +111,7 @@ app = FastAPI()
89
111
90
112
91
113
@app.on_event("startup")
92
-
asyncdefapp_startup():
114
+
asynchronousdefapp_startup():
93
115
ifnot broker.is_worker_process:
94
116
await broker.startup()
95
117
@@ -98,9 +120,10 @@ async def app_startup():
98
120
asyncdefapp_shutdown():
99
121
ifnot broker.is_worker_process:
100
122
await broker.shutdown()
101
-
102
123
```
103
124
125
+
:::
126
+
104
127
And that's it. Now you can use your taskiq tasks with functions and classes that depend on FastAPI dependencies. You can find bigger examples in the [examples repo](https://github.com/taskiq-python/examples/).
105
128
106
129
@@ -114,7 +137,6 @@ Let's imagine that you have a fixture of your application. It returns a new fast
0 commit comments