Skip to content

Commit c799172

Browse files
authored
Update Taskiq integration instructions for FastAPI
Clarify usage of TaskiqDepends with FastAPI dependencies and update code examples.
1 parent b579ae9 commit c799172

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

docs/framework_integrations/taskiq-with-fastapi.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import taskiq_fastapi
1919
broker = ZeroMQBroker()
2020

2121
taskiq_fastapi.init(broker, "my_package.application:app")
22+
2223
```
2324

2425
There are two rules to make everything work as you expect:
@@ -42,6 +43,7 @@ from typing import Any
4243

4344
def get_redis_pool(request: Request) -> Any:
4445
return request.app.state.redis_pool
46+
4547
```
4648

4749
To make it resolvable in taskiq, people should mark default fastapi dependencies (such as `Request` and `HTTPConnection`) with `TaskiqDepends`. Like this:
@@ -59,6 +61,7 @@ from taskiq import TaskiqDepends
5961

6062
async def get_redis_pool(request: Annotated[Request, TaskiqDepends()]):
6163
return request.app.state.redis_pool
64+
6265
```
6366

6467
@tab default values
@@ -70,6 +73,7 @@ from taskiq import TaskiqDepends
7073

7174
async def get_redis_pool(request: Request = TaskiqDepends()):
7275
return request.app.state.redis_pool
76+
7377
```
7478

7579
:::
@@ -111,7 +115,7 @@ app = FastAPI()
111115

112116

113117
@app.on_event("startup")
114-
asynchronous def app_startup():
118+
async def app_startup():
115119
if not broker.is_worker_process:
116120
await broker.startup()
117121

@@ -120,6 +124,7 @@ asynchronous def app_startup():
120124
async def app_shutdown():
121125
if not broker.is_worker_process:
122126
await broker.shutdown()
127+
123128
```
124129

125130
:::
@@ -137,6 +142,7 @@ Let's imagine that you have a fixture of your application. It returns a new fast
137142
@pytest.fixture
138143
def fastapi_app() -> FastAPI:
139144
return get_app()
145+
140146
```
141147

142148
Right after this fixture, we define another one.
@@ -155,6 +161,7 @@ def init_taskiq_deps(fastapi_app: FastAPI):
155161
yield
156162

157163
broker.custom_dependency_context = {}
164+
158165
```
159166

160167
This fixture has autouse flag, which means it would run on every test automatically.

0 commit comments

Comments
 (0)