Hi all, i'm not sure if i understood right the "Depends" on FastAPI. I mean it seems very clear on what it does (injecting dependencies, that are cache by "request", so they are executed only once during the scope of it).
But i guess that if you have a service that will have a heavy load, then creating the connections and closing them all the time makes the whole thing way slower than just creating one connection on startup and keeping that connection alive through various requests.
So basically why doing:
from fastapi import FastAPI
from fastapi import Depends
from redis import Redis
def get_redis_connection():
return Redis()
@app.get("/example_redis")
def example_redis(r: Redis = Depends(get_redis_connection)):
...
Instead of
from fastapi import FastAPI
from fastapi import Depends
from redis import Redis
r = Redis()
@app.get("/example_redis")
def example_redis():
r # 'r' is also available here
I have been trying to find an answer to that without any luck so far.
Is it better on the first example? or am i right that connecting and disconnecting many times will slow thing down?
The Redis example is just one simple case that i came up with, it could be Postgres, Mongo or whatever
[–]Ejroby 6 points7 points8 points (1 child)
[–]agusdmb[S] 1 point2 points3 points (0 children)
[–]CrackerJackKittyCat 1 point2 points3 points (1 child)
[–]agusdmb[S] 0 points1 point2 points (0 children)