From 7833d70d9e0b86e7b92a4d4f35eb955f782cf632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= <98187+Tronic@users.noreply.github.com> Date: Sun, 1 Mar 2020 21:41:10 +0200 Subject: [PATCH] Allow multiple workers on MacOS with Python 3.8. Fallback to single worker on Windows until pickling can be fixed. (#1798) --- sanic/app.py | 8 +++++++- sanic/server.py | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index d12dc59b..3b93b374 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1096,7 +1096,7 @@ class Sanic: stop_event: Any = None, register_sys_signals: bool = True, access_log: Optional[bool] = None, - **kwargs: Any + **kwargs: Any, ) -> None: """Run the HTTP Server and listen until keyboard interrupt or term signal. On termination, drain connections before closing. @@ -1177,6 +1177,12 @@ class Sanic: try: self.is_running = True + if workers > 1 and os.name != "posix": + logger.warn( + f"Multiprocessing is currently not supported on {os.name}," + " using workers=1 instead" + ) + workers = 1 if workers == 1: if auto_reload and os.name != "posix": # This condition must be removed after implementing diff --git a/sanic/server.py b/sanic/server.py index b9e72197..4251d674 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -1,4 +1,5 @@ import asyncio +import multiprocessing import os import sys import traceback @@ -6,7 +7,6 @@ import traceback from collections import deque from functools import partial from inspect import isawaitable -from multiprocessing import Process from signal import SIG_IGN, SIGINT, SIGTERM, Signals from signal import signal as signal_func from socket import SO_REUSEADDR, SOL_SOCKET, socket @@ -1017,9 +1017,10 @@ def serve_multiple(server_settings, workers): signal_func(SIGINT, lambda s, f: sig_handler(s, f)) signal_func(SIGTERM, lambda s, f: sig_handler(s, f)) + mp = multiprocessing.get_context("fork") for _ in range(workers): - process = Process(target=serve, kwargs=server_settings) + process = mp.Process(target=serve, kwargs=server_settings) process.daemon = True process.start() processes.append(process)