Allow fork in limited cases (#2624)
This commit is contained in:
@@ -8,11 +8,6 @@ from typing import TYPE_CHECKING
|
||||
if TYPE_CHECKING:
|
||||
from sanic import Sanic
|
||||
|
||||
try:
|
||||
from sanic_ext import Extend # type: ignore
|
||||
except ImportError:
|
||||
...
|
||||
|
||||
|
||||
def setup_ext(app: Sanic, *, fail: bool = False, **kwargs):
|
||||
if not app.config.AUTO_EXTEND:
|
||||
@@ -33,7 +28,7 @@ def setup_ext(app: Sanic, *, fail: bool = False, **kwargs):
|
||||
return
|
||||
|
||||
if not getattr(app, "_ext", None):
|
||||
Ext: Extend = getattr(sanic_ext, "Extend")
|
||||
Ext = getattr(sanic_ext, "Extend")
|
||||
app._ext = Ext(app, **kwargs)
|
||||
|
||||
return app.ext
|
||||
|
||||
@@ -3,10 +3,22 @@ import os
|
||||
import signal
|
||||
import sys
|
||||
|
||||
from typing import Awaitable
|
||||
from contextlib import contextmanager
|
||||
from typing import Awaitable, Union
|
||||
|
||||
from multidict import CIMultiDict # type: ignore
|
||||
|
||||
from sanic.helpers import Default
|
||||
|
||||
|
||||
if sys.version_info < (3, 8): # no cov
|
||||
StartMethod = Union[Default, str]
|
||||
else: # no cov
|
||||
from typing import Literal
|
||||
|
||||
StartMethod = Union[
|
||||
Default, Literal["fork"], Literal["forkserver"], Literal["spawn"]
|
||||
]
|
||||
|
||||
OS_IS_WINDOWS = os.name == "nt"
|
||||
UVLOOP_INSTALLED = False
|
||||
@@ -19,6 +31,16 @@ except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
@contextmanager
|
||||
def use_context(method: StartMethod):
|
||||
from sanic import Sanic
|
||||
|
||||
orig = Sanic.start_method
|
||||
Sanic.start_method = method
|
||||
yield
|
||||
Sanic.start_method = orig
|
||||
|
||||
|
||||
def enable_windows_color_support():
|
||||
import ctypes
|
||||
|
||||
|
||||
@@ -40,9 +40,9 @@ from sanic.application.logo import get_logo
|
||||
from sanic.application.motd import MOTD
|
||||
from sanic.application.state import ApplicationServerInfo, Mode, ServerStage
|
||||
from sanic.base.meta import SanicMeta
|
||||
from sanic.compat import OS_IS_WINDOWS, is_atty
|
||||
from sanic.compat import OS_IS_WINDOWS, StartMethod, is_atty
|
||||
from sanic.exceptions import ServerKilled
|
||||
from sanic.helpers import Default
|
||||
from sanic.helpers import Default, _default
|
||||
from sanic.http.constants import HTTP
|
||||
from sanic.http.tls import get_ssl_context, process_to_context
|
||||
from sanic.http.tls.context import SanicSSLContext
|
||||
@@ -88,6 +88,7 @@ class StartupMixin(metaclass=SanicMeta):
|
||||
state: ApplicationState
|
||||
websocket_enabled: bool
|
||||
multiplexer: WorkerMultiplexer
|
||||
start_method: StartMethod = _default
|
||||
|
||||
def setup_loop(self):
|
||||
if not self.asgi:
|
||||
@@ -692,12 +693,17 @@ class StartupMixin(metaclass=SanicMeta):
|
||||
return any(app.state.auto_reload for app in cls._app_registry.values())
|
||||
|
||||
@classmethod
|
||||
def _get_context(cls) -> BaseContext:
|
||||
method = (
|
||||
"spawn"
|
||||
if "linux" not in sys.platform or cls.should_auto_reload()
|
||||
else "fork"
|
||||
def _get_startup_method(cls) -> str:
|
||||
return (
|
||||
cls.start_method
|
||||
if not isinstance(cls.start_method, Default)
|
||||
else "spawn"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _get_context(cls) -> BaseContext:
|
||||
method = cls._get_startup_method()
|
||||
logger.debug("Creating multiprocessing context using '%s'", method)
|
||||
return get_context(method)
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user