Move uvloop setup method to its own file
This commit is contained in:
parent
877f5e6aef
commit
62b9d61fc0
@ -84,7 +84,7 @@ from sanic.response import BaseHTTPResponse, HTTPResponse
|
||||
from sanic.router import Router
|
||||
from sanic.server import AsyncioServer, HttpProtocol
|
||||
from sanic.server import Signal as ServerSignal
|
||||
from sanic.server import serve, serve_multiple, serve_single, use_uvloop
|
||||
from sanic.server import serve, serve_multiple, serve_single, try_use_uvloop
|
||||
from sanic.server.protocols.websocket_protocol import WebSocketProtocol
|
||||
from sanic.server.websockets.impl import ConnectionClosed
|
||||
from sanic.signals import Signal, SignalRouter
|
||||
@ -1103,7 +1103,7 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
|
||||
)
|
||||
|
||||
if self.config.USE_UVLOOP:
|
||||
use_uvloop()
|
||||
try_use_uvloop()
|
||||
|
||||
try:
|
||||
self.is_running = True
|
||||
|
@ -1,49 +1,10 @@
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from distutils.util import strtobool
|
||||
|
||||
from sanic.compat import OS_IS_WINDOWS
|
||||
from sanic.log import error_logger
|
||||
from sanic.models.server_types import ConnInfo, Signal
|
||||
from sanic.server.async_server import AsyncioServer
|
||||
from sanic.server.loop import try_use_uvloop
|
||||
from sanic.server.protocols.http_protocol import HttpProtocol
|
||||
from sanic.server.runners import serve, serve_multiple, serve_single
|
||||
|
||||
|
||||
def use_uvloop() -> None:
|
||||
"""
|
||||
Use uvloop instead of the default asyncio loop.
|
||||
"""
|
||||
try:
|
||||
import uvloop # type: ignore
|
||||
|
||||
if strtobool(os.environ.get("SANIC_NO_UVLOOP", "no")):
|
||||
error_logger.warning(
|
||||
"You are running Sanic using uvloop, but the "
|
||||
"'SANIC_NO_UVLOOP' environment variable (used to opt-out "
|
||||
"of installing uvloop with Sanic) is set to true. If you "
|
||||
"want to disable uvloop with Sanic, set the 'USE_UVLOOP' "
|
||||
"configuration value to false."
|
||||
)
|
||||
|
||||
if not isinstance(
|
||||
asyncio.get_event_loop_policy(), uvloop.EventLoopPolicy
|
||||
):
|
||||
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
|
||||
|
||||
except ImportError:
|
||||
if not OS_IS_WINDOWS:
|
||||
error_logger.warning(
|
||||
"You are trying to use uvloop, but uvloop is not "
|
||||
"installed in your system. In order to use uvloop "
|
||||
"you must first install it. Otherwise, you can disable "
|
||||
"uvloop completely by setting the 'USE_UVLOOP' "
|
||||
"configuration value to false. Sanic will now continue "
|
||||
"to run without using uvloop."
|
||||
)
|
||||
|
||||
|
||||
__all__ = (
|
||||
"AsyncioServer",
|
||||
"ConnInfo",
|
||||
@ -52,4 +13,5 @@ __all__ = (
|
||||
"serve",
|
||||
"serve_multiple",
|
||||
"serve_single",
|
||||
"try_use_uvloop",
|
||||
)
|
||||
|
44
sanic/server/loop.py
Normal file
44
sanic/server/loop.py
Normal file
@ -0,0 +1,44 @@
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from distutils.util import strtobool
|
||||
|
||||
from sanic.compat import OS_IS_WINDOWS
|
||||
from sanic.log import error_logger
|
||||
|
||||
|
||||
def try_use_uvloop() -> None:
|
||||
"""
|
||||
Use uvloop instead of the default asyncio loop.
|
||||
"""
|
||||
if OS_IS_WINDOWS: # uvloop is not compatible
|
||||
return
|
||||
|
||||
try:
|
||||
import uvloop # type: ignore
|
||||
except ImportError:
|
||||
error_logger.warning(
|
||||
"You are trying to use uvloop, but uvloop is not "
|
||||
"installed in your system. In order to use uvloop "
|
||||
"you must first install it. Otherwise, you can disable "
|
||||
"uvloop completely by setting the 'USE_UVLOOP' "
|
||||
"configuration value to false. Sanic will now continue "
|
||||
"to run with the default event loop."
|
||||
)
|
||||
return
|
||||
|
||||
uvloop_install_removed = strtobool(os.environ.get("SANIC_NO_UVLOOP", "no"))
|
||||
if uvloop_install_removed:
|
||||
error_logger.info(
|
||||
"You are requesting to run Sanic using uvloop, but the "
|
||||
"install-time 'SANIC_NO_UVLOOP' environment variable (used to "
|
||||
"opt-out of installing uvloop with Sanic) is set to true. If "
|
||||
"you want to prevent Sanic from overriding the event loop policy "
|
||||
"during runtime, set the 'USE_UVLOOP' configuration value to "
|
||||
"false."
|
||||
)
|
||||
|
||||
if not isinstance(
|
||||
asyncio.get_event_loop_policy(), uvloop.EventLoopPolicy
|
||||
):
|
||||
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
|
@ -9,7 +9,7 @@ from gunicorn.workers import base # type: ignore
|
||||
|
||||
from sanic.compat import UVLOOP_INSTALLED
|
||||
from sanic.log import logger
|
||||
from sanic.server import HttpProtocol, Signal, serve, use_uvloop
|
||||
from sanic.server import HttpProtocol, Signal, serve, try_use_uvloop
|
||||
from sanic.server.protocols.websocket_protocol import WebSocketProtocol
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@ except ImportError:
|
||||
ssl = None # type: ignore
|
||||
|
||||
if UVLOOP_INSTALLED:
|
||||
use_uvloop()
|
||||
try_use_uvloop()
|
||||
|
||||
|
||||
class GunicornWorker(base.Worker):
|
||||
|
Loading…
x
Reference in New Issue
Block a user