From b579ae94dd33e9d4e370e751ea2d7c78713ebca7 Mon Sep 17 00:00:00 2001 From: Shaxzodbek <134439368+Shaxzodbek16@users.noreply.github.com> Date: Thu, 5 Feb 2026 10:34:39 +0500 Subject: [PATCH 1/2] Update Taskiq with FastAPI documentation to include lifespan example --- .../taskiq-with-fastapi.md | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/docs/framework_integrations/taskiq-with-fastapi.md b/docs/framework_integrations/taskiq-with-fastapi.md index 5f7a9b56..3575cec6 100644 --- a/docs/framework_integrations/taskiq-with-fastapi.md +++ b/docs/framework_integrations/taskiq-with-fastapi.md @@ -19,7 +19,6 @@ import taskiq_fastapi broker = ZeroMQBroker() taskiq_fastapi.init(broker, "my_package.application:app") - ``` There are two rules to make everything work as you expect: @@ -43,7 +42,6 @@ from typing import Any def get_redis_pool(request: Request) -> Any: return request.app.state.redis_pool - ``` 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 async def get_redis_pool(request: Annotated[Request, TaskiqDepends()]): return request.app.state.redis_pool - ``` @tab default values @@ -73,7 +70,6 @@ from taskiq import TaskiqDepends async def get_redis_pool(request: Request = TaskiqDepends()): return request.app.state.redis_pool - ``` ::: @@ -81,6 +77,32 @@ async def get_redis_pool(request: Request = TaskiqDepends()): Also you want to call startup of your brokers somewhere. +::: tabs + +@tab Lifespan (Recommended) + +```python +from contextlib import asynccontextmanager +from fastapi import FastAPI +from your_project.taskiq import broker + + +@asynccontextmanager +async def lifespan(app: FastAPI): + # Startup + if not broker.is_worker_process: + await broker.startup() + yield + # Shutdown + if not broker.is_worker_process: + await broker.shutdown() + + +app = FastAPI(lifespan=lifespan) +``` + +@tab on_event (Deprecated) + ```python from fastapi import FastAPI from your_project.taskiq import broker @@ -89,7 +111,7 @@ app = FastAPI() @app.on_event("startup") -async def app_startup(): +asynchronous def app_startup(): if not broker.is_worker_process: await broker.startup() @@ -98,9 +120,10 @@ async def app_startup(): async def app_shutdown(): if not broker.is_worker_process: await broker.shutdown() - ``` +::: + 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/). @@ -114,7 +137,6 @@ Let's imagine that you have a fixture of your application. It returns a new fast @pytest.fixture def fastapi_app() -> FastAPI: return get_app() - ``` Right after this fixture, we define another one. @@ -133,7 +155,6 @@ def init_taskiq_deps(fastapi_app: FastAPI): yield broker.custom_dependency_context = {} - ``` This fixture has autouse flag, which means it would run on every test automatically. From c799172bfdf8deaedbabc30aabc813320eb2142a Mon Sep 17 00:00:00 2001 From: Shaxzodbek <134439368+Shaxzodbek16@users.noreply.github.com> Date: Thu, 5 Feb 2026 10:38:20 +0500 Subject: [PATCH 2/2] Update Taskiq integration instructions for FastAPI Clarify usage of TaskiqDepends with FastAPI dependencies and update code examples. --- docs/framework_integrations/taskiq-with-fastapi.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/framework_integrations/taskiq-with-fastapi.md b/docs/framework_integrations/taskiq-with-fastapi.md index 3575cec6..b0688703 100644 --- a/docs/framework_integrations/taskiq-with-fastapi.md +++ b/docs/framework_integrations/taskiq-with-fastapi.md @@ -19,6 +19,7 @@ import taskiq_fastapi broker = ZeroMQBroker() taskiq_fastapi.init(broker, "my_package.application:app") + ``` There are two rules to make everything work as you expect: @@ -42,6 +43,7 @@ from typing import Any def get_redis_pool(request: Request) -> Any: return request.app.state.redis_pool + ``` 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 async def get_redis_pool(request: Annotated[Request, TaskiqDepends()]): return request.app.state.redis_pool + ``` @tab default values @@ -70,6 +73,7 @@ from taskiq import TaskiqDepends async def get_redis_pool(request: Request = TaskiqDepends()): return request.app.state.redis_pool + ``` ::: @@ -111,7 +115,7 @@ app = FastAPI() @app.on_event("startup") -asynchronous def app_startup(): +async def app_startup(): if not broker.is_worker_process: await broker.startup() @@ -120,6 +124,7 @@ asynchronous def app_startup(): async def app_shutdown(): if not broker.is_worker_process: await broker.shutdown() + ``` ::: @@ -137,6 +142,7 @@ Let's imagine that you have a fixture of your application. It returns a new fast @pytest.fixture def fastapi_app() -> FastAPI: return get_app() + ``` Right after this fixture, we define another one. @@ -155,6 +161,7 @@ def init_taskiq_deps(fastapi_app: FastAPI): yield broker.custom_dependency_context = {} + ``` This fixture has autouse flag, which means it would run on every test automatically.