-
Notifications
You must be signed in to change notification settings - Fork 288
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
Custom ResourceFactory provider help #760
Comments
@agusmdev you dont want a session/connection per request right? I thought that what you want is that connections are reused |
@agusmdev wouldnt a possible solution be to create the session before a request, with middleware, and close it afterrwards |
@theobouwman Mmm I think that I do want 1 session per request. I'm using FastAPI and I'd like to emulate the same injection that you can do with FastAPI built-in
Yes, it's possible, but the session per request is working correctly with the implementation I have, the issue appears when you have a bunch of requests at the same time, the sessions get reused by the requests because the implementation of |
@agusmdev okay I get it. I have the issue that with this implementation (https://github.com/ets-labs/python-dependency-injector/blob/master/examples/miniapps/fastapi-sqlalchemy/webapp/database.py) each query a new session is created, which is not what we want because this takes time as you can see in the screenshot. |
What complications could arise when using this? |
How do you test this? |
Hi! I'm trying to handle SQLAlchemy sessions as a resource provider instead of using FastAPI's built-in
Depends
.Let me share some code:
The database class:
The main container where I create these resources:
So, I inject the session into the services directly (or in the future in one repository). I don't like injecting the session into the routers directly that's why I prefer not using the FastAPI
Depends
.The code provided works as expected, it creates 1 session per request and then closes it, the thing is that this doesn't work in a concurrent environment, I made some local testing with locust, and once I start having many requests executing with multi-threads (I'm using
sync
endpoints), the sessions don't work as expected, it seems that the same session is being used in more than 1 request at the same time, so I guess this is something that we could solve if we had something likeFactoryResource
instead of just aResource
that works as aSingleton
. I tried to implement this myself by creating aCustomProvider
but I couldn't make it, so if anyone has any advice I'd appreciate it.P.S: I checked all the related issues regarding FastAPI and SQLAlchemy sessions in this repo and tried some approaches but none of them worked correctly
The text was updated successfully, but these errors were encountered: