Issue
I'm trying to get client connection to Redis in my application
def create_app():
app = FastAPI(docs_url='/')
task_queue: Queue
@app.on_event("startup")
async def startup_event(redis_conn: redis.asyncio.Redis = Depends(get_async_redis_client)):
nonlocal task_queue
task_queue = Queue("task_queue", connection=redis_conn)
@app.post("/add_data")
async def add_data(data: str):
task_queue.enqueue(process_data, data)
return {"message": "Book in processing"}
@app.get("/get_data")
async def get_data():
return {"data": "kek"}
return app
def main():
uvicorn.run(
f"{__name__}:create_app",
host='0.0.0.0', port=8888,
reload=True
)
if __name__ == '__main__':
print("")
main()
Generator get_async_redis_client got from:
redis_pool = redis.asyncio.ConnectionPool.from_url(f"redis://{REDIS_HOST}:{REDIS_PORT}")
async def get_async_redis_client() -> AsyncGenerator[Redis, None]:
async with Redis.from_pool(redis_pool) as client:
yield client
When i do POST request to "\add_data" i got an error
File "C:\Users\Степан\PycharmProjects\testToBaum\src\main.py", line 28, in add_data
task_queue.enqueue(process_data, data)
File "C:\Users\Степан\PycharmProjects\testToBaum\venv\Lib\site-packages\rq\queue.py", line 972, in enqueue
return self.enqueue_call(
^^^^^^^^^^^^^^^^^^
File "C:\Users\Степан\PycharmProjects\testToBaum\venv\Lib\site-packages\rq\queue.py", line 721, in enqueue_call
return self.enqueue_job(job, pipeline=pipeline, at_front=at_front)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Степан\PycharmProjects\testToBaum\venv\Lib\site-packages\rq\queue.py", line 1095, in enqueue_job
return self._enqueue_job(job, pipeline=pipeline, at_front=at_front)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Степан\PycharmProjects\testToBaum\venv\Lib\site-packages\rq\queue.py", line 1111, in _enqueue_job
pipe = pipeline if pipeline is not None else self.connection.pipeline()
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Depends' object has no attribute 'pipeline'
I thought that when i'm using Depends() with generator then "redis_conn" must be redis.asyncio.Redis object.
The same case with postgresql connection worked before
Solution
Unfortunately you can't use dependencies in startup
event.
You can use this as a workaround:
def create_app():
task_queue: Queue
async def lifespann(app):
get_async_redis_client_real = app.dependency_overrides.get(get_async_redis_client, get_async_redis_client)
async for redis_conn in get_async_redis_client_real():
nonlocal task_queue
task_queue = Queue("task_queue", connection=redis_conn)
yield
app = FastAPI(
docs_url='/',
lifespan=lifespann
)
@app.post("/add_data")
async def add_data(data: str):
task_queue.enqueue(process_data, data)
return {"message": "Book in processing"}
@app.get("/get_data")
async def get_data():
return {"data": "kek"}
return app
Answered By - Yurii Motov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.