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

Forward gunicorn reload parameter to uvicorn worker #1193

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion uvicorn/workers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import asyncio
import logging
import os
import signal
import sys
import threading
import time
from typing import Any

from gunicorn.arbiter import Arbiter
Expand All @@ -11,6 +14,20 @@
from uvicorn.main import Server


class ReloaderThread(threading.Thread):
def __init__(self, worker: "UvicornWorker", sleep_interval: float = 1.0):
super().__init__()
self.setDaemon(True)
self._worker = worker
self._interval = sleep_interval

def run(self) -> None:
while True:
if not self._worker.alive:
os.kill(os.getpid(), signal.SIGINT)
time.sleep(self._interval)


class UvicornWorker(Worker):
"""
A worker class for Gunicorn that interfaces with an ASGI consumer callable,
Expand All @@ -20,7 +37,8 @@ class UvicornWorker(Worker):
CONFIG_KWARGS = {"loop": "auto", "http": "auto"}

def __init__(self, *args: Any, **kwargs: Any) -> None:
super(UvicornWorker, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self._reloader_thread = ReloaderThread(self)

logger = logging.getLogger("uvicorn.error")
logger.handlers = self.log.error_log.handlers
Expand Down Expand Up @@ -80,6 +98,9 @@ async def _serve(self) -> None:
sys.exit(Arbiter.WORKER_BOOT_ERROR)

def run(self) -> None:
if self.cfg.reload:
self._reloader_thread.start()

if sys.version_info >= (3, 7):
return asyncio.run(self._serve())
return asyncio.get_event_loop().run_until_complete(self._serve())
Expand Down