This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
getting task attached to a different loop runtime error when using fastapi with gunicron and motor #3855
Comments
This is a quite common issue when using Motor (unfortunately, their implementation doesn't always play well with some event loops patterns). I think the problem comes from here: def get_users_db() -> motor_asyncio.AsyncIOMotorDatabase:
return mongo_client.devdb I'm not really sure about Motor implements this underneath, but I guess it will give you different instances for the same database ; which may be attached to a different event loop. Maybe try something like this: from motor import motor_asyncio
mongo_uri = "mongodb://mongoUser:***/?authSource=admin"
mongo_client = motor_asyncio.AsyncIOMotorClient(mongo_uri)
users_db = mongo_client.devdb
def get_users_db() -> motor_asyncio.AsyncIOMotorDatabase:
return users_db |
You should init Motor client as a startup event |
thanks @frankie567 and @DavidBord for taking a look into this problem. solution by @frankie567 produces the same error, that is motor gets attached to a different loop than fastapi. This problem appears to be in newer version of either motor, gunicorn or fastapi as initializing motor outside startup was working fine prevesouly, also it works if I use uvicorn as a server instead of gunicorn (i don't know which one is better, i use gunicorn only because that is suggested in fastapi and uvicorn docs) This might not be the place to ask, but can you suggest how to handle refrence to mongodb client or db when we init motor in app's startup event (using request.app.client seems unmanageable). Any help/pointer would be nice |
The problem is with |
@xcoder01 the startup event handler inits |
you should define in start up , like this : app = FastAPI()
app.add_event_handler("startup", start_app)
async def start_app():
await Mongo().get_connection()
from motor.motor_asyncio import AsyncIOMotorClient
from config import CONNECTION_STRING, DB_NAME
class Mongo:
client = None
db = None
async def get_connection(self) -> AsyncIOMotorClient:
if not Mongo.client:
Mongo.client = AsyncIOMotorClient(CONNECTION_STRING) now you can use mongo client every where you want!!! |
I have the same problem but only for the tests which are running under pytest. The app under uvicorn works fine. The problem appears when I switch from fastapi 0.68.x to 0.69.x or higher. It seems that the motor event loop isn't initialized properly in the tests print(asyncio.get_event_loop(), db.client.io_loop) under pytest: print(asyncio.get_event_loop(), db.client.io_loop) under uvicorn: I think it has to do with the breaking change introduced in 0.69.0:
motor isn't for some reason using the existing event loop and might created it's own hence I get
I tried also |
That is the problem: encode/starlette#1315
works for me |
@CanD42 I was facing this using fastapi + fsspec's adlfs and solved the same way. Thanks
|
Thanks for the help here everyone! 👏 🙇 Thanks for reporting back and closing the issue @xcoder01 👍 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
i am trying to create an app with mongodb client
motor
andfastapi
. I created seperate routes and a seperate file to setup mongodb client. Running this withuvicorn
works fine and i get results after querying the database but when i deploy the app withgunicorn
and again use the route, i get the following error.I tried bunch of methods, like assigning a loop to motor client, changing uvworker to use asyncio loop etc. i don't know where to post this error but here i am
Operating System
Linux
Operating System Details
Debian 11
FastAPI Version
0.68.1
Python Version
Python 3.9.2
Additional Context
motor==2.2.6
gunicorn==20.1
this issue only happens with gunicorn as server and only when accessing mongodb from a fastapi route (i checked running db query in the mongo file and it works)
this my first ever time creating an issue, so please guide me i did something wrong.
The text was updated successfully, but these errors were encountered: