{"id":27603547,"url":"https://github.com/taskiq-python/taskiq-fastapi","last_synced_at":"2025-04-22T19:17:31.923Z","repository":{"id":164169514,"uuid":"618939860","full_name":"taskiq-python/taskiq-fastapi","owner":"taskiq-python","description":"FastAPI integration for taskiq","archived":false,"fork":false,"pushed_at":"2025-04-20T16:30:15.000Z","size":140,"stargazers_count":38,"open_issues_count":5,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-20T17:34:18.604Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/taskiq-python.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-03-25T19:38:57.000Z","updated_at":"2025-04-20T16:30:14.000Z","dependencies_parsed_at":"2023-09-21T15:33:50.887Z","dependency_job_id":"b73c42c6-0720-48a8-8e92-c389d6486f49","html_url":"https://github.com/taskiq-python/taskiq-fastapi","commit_stats":{"total_commits":19,"total_committers":2,"mean_commits":9.5,"dds":"0.052631578947368474","last_synced_commit":"9a43328ab892ddf64dc1578e12a1a80bb33047f0"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taskiq-python%2Ftaskiq-fastapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taskiq-python%2Ftaskiq-fastapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taskiq-python%2Ftaskiq-fastapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taskiq-python%2Ftaskiq-fastapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taskiq-python","download_url":"https://codeload.github.com/taskiq-python/taskiq-fastapi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249981844,"owners_count":21355579,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-22T19:17:31.404Z","updated_at":"2025-04-22T19:17:31.908Z","avatar_url":"https://github.com/taskiq-python.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Taskiq + FastAPI\n\nThis repository has a code to integrate FastAPI with taskiq easily.\n\nTaskiq and FastAPI both have dependencies and this library makes it possible to depend on\n`fastapi.Request` or `starlette.requests.HTTPConnection` in taskiq tasks.\n\nWith this library you can easily re-use your fastapi dependencies in taskiq functions.\n\n## How does it work?\n\nIt adds startup functions to broker so it imports your fastapi application\nand creates a single worker-wide Request and HTTPConnection objects that you depend on.\n\nTHIS REQUEST IS NOT RELATED TO THE ACTUAL REQUESTS IN FASTAPI!\nThis request won't have actual data about the request you were handling while sending task.\n\n## Usage\n\nHere we have an example of function that is being used by both taskiq's task and\nfastapi's handler function.\n\nI have a script called `test_script.py` so my app can be found at `test_script:app`.\nWe use strings to resolve application to bypass circular imports.\n\nAlso, as you can see, we use `TaskiqDepends` for Request. That's because\ntaskiq dependency resolver must know that this type must be injected. FastAPI disallow\nDepends for Request type. That's why we use `TaskiqDepends`.\n\n```python\nfrom fastapi import FastAPI, Request\nfrom pydantic import BaseModel\nfrom redis.asyncio import ConnectionPool, Redis\nfrom fastapi import Depends as FastAPIDepends\nfrom taskiq import TaskiqDepends\nimport taskiq_fastapi\nfrom taskiq import ZeroMQBroker\n\nbroker = ZeroMQBroker()\n\napp = FastAPI()\n\n\n@app.on_event(\"startup\")\nasync def app_startup():\n    #####################\n    # IMPORTANT NOTE    #\n    #####################\n    # If you won't check that this is not\n    # a worker process, you'll\n    # create an infinite recursion. Because in worker processes\n    # fastapi startup will be called.\n    if not broker.is_worker_process:\n        print(\"Starting broker\")\n        await broker.startup()\n    print(\"Creating redis pool\")\n    app.state.redis_pool = ConnectionPool.from_url(\"redis://localhost\")\n\n\n@app.on_event(\"shutdown\")\nasync def app_shutdown():\n    #####################\n    # IMPORTANT NOTE    #\n    #####################\n    # If you won't check that this is not\n    # a worker process, you'll\n    # create an infinite recursion. Because in worker processes\n    # fastapi startup will be called.\n    if not broker.is_worker_process:\n        print(\"Shutting down broker\")\n        await broker.shutdown()\n    print(\"Stopping redis pool\")\n    await app.state.redis_pool.disconnect()\n\n\n# Here we call our magic function.\ntaskiq_fastapi.init(broker, \"test_script:app\")\n\n\n# We use TaskiqDepends here, because if we use FastAPIDepends fastapi\n# initialization will fail.\ndef get_redis_pool(request: Request = TaskiqDepends()) -\u003e ConnectionPool:\n    return request.app.state.redis_pool\n\n\n@broker.task\nasync def my_redis_task(\n    key: str,\n    val: str,\n    # Here we depend using TaskiqDepends.\n    # Please use TaskiqDepends for all tasks to be resolved correctly.\n    # Or dependencies won't be injected.\n    pool: ConnectionPool = TaskiqDepends(get_redis_pool),\n):\n    async with Redis(connection_pool=pool) as redis:\n        await redis.set(key, val)\n        print(\"Value set.\")\n\n\nclass MyVal(BaseModel):\n    key: str\n    val: str\n\n\n@app.post(\"/val\")\nasync def setval_endpoint(val: MyVal) -\u003e None:\n    await my_redis_task.kiq(\n        key=val.key,\n        val=val.val,\n    )\n    print(\"Task sent\")\n\n\n@app.get(\"/val\")\nasync def getval_endpoint(\n    key: str,\n    pool: ConnectionPool = FastAPIDepends(get_redis_pool),\n) -\u003e str:\n    async with Redis(connection_pool=pool, decode_responses=True) as redis:\n        return await redis.get(key)\n\n```\n\n## Manually update dependency context\n\nWhen using `InMemoryBroker` it may be required to update the dependency context manually. This may also be useful when setting up tests.\n\n```py\nimport taskiq_fastapi\nfrom taskiq import InMemoryBroker\n\nbroker = InMemoryBroker()\n\napp = FastAPI()\n\ntaskiq_fastapi.init(broker, \"test_script:app\")\ntaskiq_fastapi.populate_dependency_context(broker, app)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaskiq-python%2Ftaskiq-fastapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaskiq-python%2Ftaskiq-fastapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaskiq-python%2Ftaskiq-fastapi/lists"}