| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import platform |
| 4 |
import signal |
| 5 |
import time |
| 6 |
from multiprocessing import get_context |
| 7 |
from multiprocessing.context import BaseContext |
| 8 |
from multiprocessing.process import BaseProcess |
| 9 |
from multiprocessing.synchronize import Event as EventType |
| 10 |
from pickle import PicklingError |
| 11 |
from typing import Any, List |
| 12 |
|
| 13 |
from .config import Config, Sockets |
| 14 |
from .typing import WorkerFunc |
| 15 |
from .utils import load_application, wait_for_changes, write_pid_file |
| 16 |
|
| 17 |
|
| 18 |
def run(config: Config) -> None: |
| 19 |
if config.pid_path is not None: |
| 20 |
write_pid_file(config.pid_path) |
| 21 |
|
| 22 |
worker_func: WorkerFunc |
| 23 |
if config.worker_class == "asyncio": |
| 24 |
from .asyncio.run import asyncio_worker |
| 25 |
|
| 26 |
worker_func = asyncio_worker |
| 27 |
elif config.worker_class == "uvloop": |
| 28 |
from .asyncio.run import uvloop_worker |
| 29 |
|
| 30 |
worker_func = uvloop_worker |
| 31 |
elif config.worker_class == "trio": |
| 32 |
from .trio.run import trio_worker |
| 33 |
|
| 34 |
worker_func = trio_worker |
| 35 |
else: |
| 36 |
raise ValueError(f"No worker of class {config.worker_class} exists") |
| 37 |
|
| 38 |
sockets = config.create_sockets() |
| 39 |
|
| 40 |
if config.use_reloader and config.workers == 0: |
| 41 |
raise RuntimeError("Cannot reload without workers") |
| 42 |
|
| 43 |
if config.use_reloader or config.workers == 0: |
| 44 |
# Load the application so that the correct paths are checked for |
| 45 |
# changes, but only when the reloader is being used. |
| 46 |
load_application(config.application_path, config.wsgi_max_body_size) |
| 47 |
|
| 48 |
if config.workers == 0: |
| 49 |
worker_func(config, sockets) |
| 50 |
else: |
| 51 |
ctx = get_context("spawn") |
| 52 |
|
| 53 |
active = True |
| 54 |
while active: |
| 55 |
# Ignore SIGINT before creating the processes, so that they |
| 56 |
# inherit the signal handling. This means that the shutdown |
| 57 |
# function controls the shutdown. |
| 58 |
signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 59 |
|
| 60 |
shutdown_event = ctx.Event() |
| 61 |
processes = start_processes(config, worker_func, sockets, shutdown_event, ctx) |
| 62 |
|
| 63 |
def shutdown(*args: Any) -> None: |
| 64 |
nonlocal active, shutdown_event |
| 65 |
shutdown_event.set() |
| 66 |
active = False |
| 67 |
|
| 68 |
for signal_name in {"SIGINT", "SIGTERM", "SIGBREAK"}: |
| 69 |
if hasattr(signal, signal_name): |
| 70 |
signal.signal(getattr(signal, signal_name), shutdown) |
| 71 |
|
| 72 |
if config.use_reloader: |
| 73 |
wait_for_changes(shutdown_event) |
| 74 |
shutdown_event.set() |
| 75 |
# Recreate the sockets to be used again in the next |
| 76 |
# iteration of the loop. |
| 77 |
sockets = config.create_sockets() |
| 78 |
else: |
| 79 |
active = False |
| 80 |
|
| 81 |
for process in processes: |
| 82 |
process.join() |
| 83 |
for process in processes: |
| 84 |
process.terminate() |
| 85 |
|
| 86 |
for sock in sockets.secure_sockets: |
| 87 |
sock.close() |
| 88 |
for sock in sockets.insecure_sockets: |
| 89 |
sock.close() |
| 90 |
|
| 91 |
|
| 92 |
def start_processes( |
| 93 |
config: Config, |
| 94 |
worker_func: WorkerFunc, |
| 95 |
sockets: Sockets, |
| 96 |
shutdown_event: EventType, |
| 97 |
ctx: BaseContext, |
| 98 |
) -> List[BaseProcess]: |
| 99 |
processes = [] |
| 100 |
for _ in range(config.workers): |
| 101 |
process = ctx.Process( # type: ignore |
| 102 |
target=worker_func, |
| 103 |
kwargs={"config": config, "shutdown_event": shutdown_event, "sockets": sockets}, |
| 104 |
) |
| 105 |
process.daemon = True |
| 106 |
try: |
| 107 |
process.start() |
| 108 |
except PicklingError as error: |
| 109 |
raise RuntimeError( |
| 110 |
"Cannot pickle the config, see https://docs.python.org/3/library/pickle.html#pickle-picklable" # noqa: E501 |
| 111 |
) from error |
| 112 |
processes.append(process) |
| 113 |
if platform.system() == "Windows": |
| 114 |
time.sleep(0.1) |
| 115 |
return processes |