FastAPI won't start with aiojobs and gunicorn
verhovensky opened this issue · 3 comments
FastAPI won't start with aiojobs and gunicorn
Steps to reproduce
-
Install fastapi & dependancies
-
Install aiojobs
-
Make some simple endpoints use your created scheduler
(say i have sheduler.py where instance of Scheduler class is imported in routes and i call Scheduler instance methods there )
# scheduler.py
scheduler = Scheduler(limit=10000, pending_limit=100000)
# some_endpoint.py
from scheduler import scheduler
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from schemas import TaskListSchema
router = APIRouter(prefix="/admin")
@router.get("/get_task_statistics", response_model=TaskListSchema)
async def get_scheduler_statistics():
...
- Try to run with gunicorn
gunicorn verification_code.main:app --workers=2 --worker-class=uvicorn.workers.UvicornWorker --bind="0.0.0.0:8000"
(with single worker it also fails)
Results in the following behavior:
LOG
[2023-01-26 14:44:32 +0600] [1047680] [INFO] Starting gunicorn 20.1.0
[2023-01-26 14:44:32 +0600] [1047680] [INFO] Listening at: http://0.0.0.0:8000 (1047680)
[2023-01-26 14:44:32 +0600] [1047680] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2023-01-26 14:44:32 +0600] [1047681] [INFO] Booting worker with pid: 1047681
[2023-01-26 14:44:32 +0600] [1047682] [INFO] Booting worker with pid: 1047682
[2023-01-26 14:44:32 +0600] [1047681] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/uvicorn/workers.py", line 66, in init_process
super(UvicornWorker, self).init_process()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/util.py", line 359, in import_app
mod = importlib.import_module(module)
File "/usr/local/lib/python3.10/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1050, in _gcd_import
File "", line 1027, in _find_and_load
File "", line 1006, in _find_and_load_unlocked
File "", line 688, in _load_unlocked
File "", line 883, in exec_module
File "", line 241, in _call_with_frames_removed
File "/home/bulat/PycharmProjects/my-app/my_app/main.py", line 12, in
from my_app.api.routers import admin, healthcheck, request_code, verify_code
File "/home/bulat/PycharmProjects/my-app/my_app/api/routers/admin.py", line 4, in
from my_app.scheduler import get_running_tasks, scheduler
File "/home/bulat/PycharmProjects/my-app/my_app/scheduler.py", line 5, in
scheduler = Scheduler(limit=10000, pending_limit=100000)
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/aiojobs/_scheduler.py", line 42, in init
loop = asyncio.get_running_loop()
RuntimeError: no running event loop
[2023-01-26 14:44:32 +0600] [1047681] [INFO] Worker exiting (pid: 1047681)
[2023-01-26 14:44:32 +0600] [1047682] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/uvicorn/workers.py", line 66, in init_process
super(UvicornWorker, self).init_process()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/gunicorn/util.py", line 359, in import_app
mod = importlib.import_module(module)
File "/usr/local/lib/python3.10/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1050, in _gcd_import
File "", line 1027, in _find_and_load
File "", line 1006, in _find_and_load_unlocked
File "", line 688, in _load_unlocked
File "", line 883, in exec_module
File "", line 241, in _call_with_frames_removed
File "/home/bulat/PycharmProjects/my-app/my_app/main.py", line 12, in
from my_app.api.routers import admin, healthcheck, request_code, verify_code
File "/home/bulat/PycharmProjects/my-app/my_app/api/routers/admin.py", line 4, in
from my_app.scheduler import get_running_tasks, scheduler
File "/home/bulat/PycharmProjects/my-app/my_app/scheduler.py", line 5, in
scheduler = Scheduler(limit=10000, pending_limit=100000)
File "/home/bulat/PycharmProjects/my-app/.venv/lib/python3.10/site-packages/aiojobs/_scheduler.py", line 42, in init
loop = asyncio.get_running_loop()
RuntimeError: no running event loop
[2023-01-26 14:44:32 +0600] [1047682] [INFO] Worker exiting (pid: 1047682)
[2023-01-26 14:44:32 +0600] [1047680] [WARNING] Worker with pid 1047682 was terminated due to signal 15
[2023-01-26 14:44:32 +0600] [1047680] [INFO] Shutting down: Master
[2023-01-26 14:44:32 +0600] [1047680] [INFO] Reason: Worker failed to boot.
Link to log on pastebin: https://pastebin.com/Vw7u8VBL
pip freeze: https://pastebin.com/tDQTjdQj
File "/home/bulat/PycharmProjects/my-app/my_app/scheduler.py", line 5, in
scheduler = Scheduler(limit=10000, pending_limit=100000)
You've tried to create it as a global?
Documentation says it must be created within a running loop. i.e. For a FastAPI application (or aiohttp) you should create it in an on_startup function.
@Dreamsorcerer
Then how do i use it in endpoint's code ?
The same as anything else in a web application; add it to the app object, or use FastAPI's questionable dependency injection.
e.g. How do you use a DB connection in your endpoint code?