Skip to content
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

[need help]: gunicorn+fastapi+beanie raise RuntimeError #245

Closed
hd10180 opened this issue Apr 24, 2022 · 1 comment
Closed

[need help]: gunicorn+fastapi+beanie raise RuntimeError #245

hd10180 opened this issue Apr 24, 2022 · 1 comment

Comments

@hd10180
Copy link

hd10180 commented Apr 24, 2022

i use fastapi + beanie, everything works perfect when running with uvicorn,
it raise RuntimeError(maybe cause by asyncio?) when using gunicorn to get more worker

i use uvicorn web_boost:app --host 127.0.0.1 --port 3344 --reload on dev env,
use gunicorn web_boost:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:3344 when error raise

i use fastapi lifespan method to init model, here is my code:

@app.on_event("startup")
async def boost_beanie():
    from app.core.session import fetch_db

    _db = fetch_db()
    app.state.db = _db
    from app.models.entity import __beanie_models__

    await init_beanie(database=_db, document_models=__beanie_models__)

the error logs here:

ERROR    2022-04-24 11:15:28.052 - uvicorn.lifespan.on:send - Traceback (most recent call last):
  File "/home/zeara/miniconda3/envs/lunar/lib/python3.8/site-packages/starlette/routing.py", line 621, in lifespan
    async with self.lifespan_context(app):
  File "/home/zeara/miniconda3/envs/lunar/lib/python3.8/site-packages/starlette/routing.py", line 518, in __aenter__
    await self._router.startup()
  File "/home/zeara/miniconda3/envs/lunar/lib/python3.8/site-packages/starlette/routing.py", line 598, in startup
    await handler()
  File "/home/zeara/proj/platform/app/core/boost.py", line 85, in boost_beanie
    await init_beanie(database=_db, document_models=__beanie_models__)
  File "/home/zeara/miniconda3/envs/lunar/lib/python3.8/site-packages/beanie/odm/utils/general.py", line 76, in init_beanie
    await asyncio.gather(*collection_inits)
  File "/home/zeara/miniconda3/envs/lunar/lib/python3.8/site-packages/beanie/odm/documents.py", line 1126, in init_model
    await cls.init_settings(
  File "/home/zeara/miniconda3/envs/lunar/lib/python3.8/site-packages/beanie/odm/documents.py", line 1093, in init_settings
    cls._document_settings = await DocumentSettings.init(
  File "/home/zeara/miniconda3/envs/lunar/lib/python3.8/site-packages/beanie/odm/settings/general.py", line 17, in init
    collection_settings = await CollectionSettings.init(
  File "/home/zeara/miniconda3/envs/lunar/lib/python3.8/site-packages/beanie/odm/settings/collection.py", line 71, in init
    build_info = await database.command({"buildInfo": 1})
RuntimeError: Task <Task pending name='Task-3' coro=<Document.init_model() running at /home/zeara/miniconda3/envs/lunar/lib/python3.8/site-packages/beanie/odm/documents.py:1126> cb=[gather.<locals>._done_callback() at /home/zeara/miniconda3/envs/lunar/lib/python3.8/asyncio/tasks.py:769]> got Future <Future pending cb=[_chain_future.<locals>._call_check_cancel() at /home/zeara/miniconda3/envs/lunar/lib/python3.8/asyncio/futures.py:360]> attached to a different loop

ERROR    2022-04-24 11:15:28.052 - uvicorn.lifespan.on:startup - Application startup failed. Exiting.
[2022-04-24 11:15:28 +0800] [15190] [INFO] Worker exiting (pid: 15190)
[2022-04-24 11:15:28 +0800] [15185] [WARNING] Worker with pid 15189 was terminated due to signal 15
[2022-04-24 11:15:28 +0800] [15185] [WARNING] Worker with pid 15187 was terminated due to signal 15
[2022-04-24 11:15:28 +0800] [15185] [WARNING] Worker with pid 15190 was terminated due to signal 15
@hd10180
Copy link
Author

hd10180 commented Apr 24, 2022

problem solved!
i find this issue tiangolo/fastapi#3855 (comment) work for me.

# my case, in session.py
db: AsyncIOMotorClient = MotorMongo.make_conn(MongoConfig(**MONGODB_CONFIG))
db.get_io_loop = asyncio.get_event_loop

def fetch_db() -> AsyncIOMotorDatabase:
    return db[MONGODB_CONFIG["authSource"]]

# init beanie
@app.on_event("startup")
async def boost_beanie():
    from app.core.session import fetch_db

    _db = fetch_db()
    app.state.db = _db
    from app.models.entity import __beanie_models__

    await init_beanie(database=_db, document_models=__beanie_models__)

@hd10180 hd10180 closed this as completed Apr 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant