on-internet
← Community

What is the recommended way to instantiate and pass around a redis client with FastAPI

Stack Overflow ·

I'm using FastAPI with Redis. My app looks something like this from fastapi import FastAPI import redis # Instantiate redis client r = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True) # Instantiate fastapi app app = FastAPI() @app.get("/foo/") async def foo(): x = r.get("foo") return {"message": x} @app.get("/bar/") async def bar(): x = r.get("bar") return {"message": x} Is it bad practice to create r as a module-scoped variable like this? If so what are the drawbacks? In Tiangolo's tutorial on setting up a SQL database connection he uses a dependency, which I guess in my

Comments

Sign in or sign up to comment and upvote.

No comments yet.