-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is async version of redis-py coroutine safe? #2835
Comments
One can never guarantee coroutine safety. But we definitely aim for it - and shield various parse response calls, due to an issue in the past. Generally put, you should avoid globals. As I don't develop (yet) with Redis + FastAPI, I don't have an ideal pattern to share unfortunately. |
@chayim Thank you for answering my question. Now it's clearer that global async-redis-client can not guarantee So I am going to make each handlers use exclusive async-redis-client instances. It seems that there are three options for doing that.
I guess,
So I provisionally conclude that option 2. is the best design decision. If you have any idea please let me know. Sincerely, |
We use case 2 on several projects for a year without troubles (except #2831 with 4.6.0 release) # app/db/redis.py
async def get_redis_connection():
redis = Redis.from_url(settings.REDIS_URL)
return redis
# app/routes/someting.py
@app.put('/some/path')
async def handle_put(key: str, value: str):
...
redis_conn = await get_redis_connection()
await redis_conn.set(key, value)
await redis_conn.close()
... We also have some workers where we initialise redis connection in the first function and then pass the |
@nsteinmetz hey mate, just wonder why not do like:
As |
I would say it's like globals - you can use it but at your own risks ;-) I'm ok with establishing Matter of taste I would say ; I prefer the least surprise driven development and lose a few ms ;-) |
oh yea you are right. Thanks a lot mate. |
I am a little confused by the question and the answer here. Obviously, your code can create race conditions in the logic, and you need to keep that in mind (i.e. where you see an The global shouldn't make any difference in this regard, that just seems like a bit of general advice on your programming. For FastAPI, they usually recommend some dependency injection thing in their docs (I think that's a bad idea though, as you won't know that the Redis login is wrong until you've deployed the app and user requests start coming in and failing). In aiohttp, we'd just add it to the app object in a cleanup_ctx:
Or similar, and then it can easily be accessed in your handlers without any globals. |
Hello
I am wondering that async version of redis-py supports coroutine-safety.
I created one redis client instance as a global, and then I used it without any
locking primitives in many coroutines, like in FastAPI request handlers.
I didn't experience some kind of bugs with this scenario, but I just want to
make sure that coroutine-safety for redis-py is guranteed.
Thanks,
The text was updated successfully, but these errors were encountered: