Skip to content

Commit 72d2882

Browse files
authored
Added Fastapi Lifespan to docs (#589)
1 parent 281da9b commit 72d2882

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

docs/framework_integrations/taskiq-with-fastapi.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ async def get_redis_pool(request: Request = TaskiqDepends()):
8181

8282
Also you want to call startup of your brokers somewhere.
8383

84+
::: tabs
85+
86+
@tab Lifespan (Recommended)
87+
88+
```python
89+
from contextlib import asynccontextmanager
90+
from fastapi import FastAPI
91+
from your_project.taskiq import broker
92+
93+
94+
@asynccontextmanager
95+
async def lifespan(app: FastAPI):
96+
# Startup
97+
if not broker.is_worker_process:
98+
await broker.startup()
99+
yield
100+
# Shutdown
101+
if not broker.is_worker_process:
102+
await broker.shutdown()
103+
104+
105+
app = FastAPI(lifespan=lifespan)
106+
```
107+
108+
@tab on_event (Deprecated)
109+
84110
```python
85111
from fastapi import FastAPI
86112
from your_project.taskiq import broker
@@ -101,6 +127,8 @@ async def app_shutdown():
101127

102128
```
103129

130+
:::
131+
104132
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/).
105133

106134

0 commit comments

Comments
 (0)