Skip to content

Commit b579ae9

Browse files
committed
Update Taskiq with FastAPI documentation to include lifespan example
1 parent eb29304 commit b579ae9

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

docs/framework_integrations/taskiq-with-fastapi.md

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

2121
taskiq_fastapi.init(broker, "my_package.application:app")
22-
2322
```
2423

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

4443
def get_redis_pool(request: Request) -> Any:
4544
return request.app.state.redis_pool
46-
4745
```
4846

4947
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
6159

6260
async def get_redis_pool(request: Annotated[Request, TaskiqDepends()]):
6361
return request.app.state.redis_pool
64-
6562
```
6663

6764
@tab default values
@@ -73,14 +70,39 @@ from taskiq import TaskiqDepends
7370

7471
async def get_redis_pool(request: Request = TaskiqDepends()):
7572
return request.app.state.redis_pool
76-
7773
```
7874

7975
:::
8076

8177

8278
Also you want to call startup of your brokers somewhere.
8379

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+
async def lifespan(app: FastAPI):
92+
# Startup
93+
if not broker.is_worker_process:
94+
await broker.startup()
95+
yield
96+
# Shutdown
97+
if not broker.is_worker_process:
98+
await broker.shutdown()
99+
100+
101+
app = FastAPI(lifespan=lifespan)
102+
```
103+
104+
@tab on_event (Deprecated)
105+
84106
```python
85107
from fastapi import FastAPI
86108
from your_project.taskiq import broker
@@ -89,7 +111,7 @@ app = FastAPI()
89111

90112

91113
@app.on_event("startup")
92-
async def app_startup():
114+
asynchronous def app_startup():
93115
if not broker.is_worker_process:
94116
await broker.startup()
95117

@@ -98,9 +120,10 @@ async def app_startup():
98120
async def app_shutdown():
99121
if not broker.is_worker_process:
100122
await broker.shutdown()
101-
102123
```
103124

125+
:::
126+
104127
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/).
105128

106129

@@ -114,7 +137,6 @@ Let's imagine that you have a fixture of your application. It returns a new fast
114137
@pytest.fixture
115138
def fastapi_app() -> FastAPI:
116139
return get_app()
117-
118140
```
119141

120142
Right after this fixture, we define another one.
@@ -133,7 +155,6 @@ def init_taskiq_deps(fastapi_app: FastAPI):
133155
yield
134156

135157
broker.custom_dependency_context = {}
136-
137158
```
138159

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

0 commit comments

Comments
 (0)