Create UVLOOP_INSTALLED constant

This commit is contained in:
prryplatypus 2021-10-23 20:39:57 +02:00
parent eb2a264404
commit 2e492f94e6
No known key found for this signature in database
GPG Key ID: 6687E128FB70819B
4 changed files with 22 additions and 29 deletions

View File

@ -50,7 +50,7 @@ from sanic.asgi import ASGIApp
from sanic.base import BaseSanic
from sanic.blueprint_group import BlueprintGroup
from sanic.blueprints import Blueprint
from sanic.compat import OS_IS_WINDOWS
from sanic.compat import OS_IS_WINDOWS, UVLOOP_INSTALLED
from sanic.config import BASE_LOGO, SANIC_PREFIX, Config
from sanic.exceptions import (
InvalidUsage,
@ -1421,10 +1421,8 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
def _configure_event_loop(self):
if self.config.USE_UVLOOP and not OS_IS_WINDOWS:
uvloop_success = use_uvloop()
if uvloop_success is False:
error_logger.warning(
if not UVLOOP_INSTALLED:
return 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 "
@ -1433,7 +1431,8 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
"to run without using uvloop."
)
elif strtobool(os.environ.get("SANIC_NO_UVLOOP", "no")):
use_uvloop()
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 "

View File

@ -8,6 +8,13 @@ from multidict import CIMultiDict # type: ignore
OS_IS_WINDOWS = os.name == "nt"
UVLOOP_INSTALLED = False
try:
import uvloop # type: ignore # noqa
UVLOOP_INSTALLED = True
except ImportError:
pass
class Header(CIMultiDict):

View File

@ -8,19 +8,14 @@ from sanic.server.runners import serve, serve_multiple, serve_single
def use_uvloop() -> bool:
"""
Use uvloop (if available) instead of the default
asyncio loop.
Use uvloop instead of the default asyncio loop.
"""
try:
import uvloop # type: ignore
import uvloop # type: ignore
if not isinstance(
asyncio.get_event_loop_policy(), uvloop.EventLoopPolicy
):
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
return False
return True
if not isinstance(
asyncio.get_event_loop_policy(), uvloop.EventLoopPolicy
):
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
__all__ = (

View File

@ -9,6 +9,7 @@ from unittest.mock import Mock, patch
import pytest
from sanic import Sanic
from sanic.compat import UVLOOP_INSTALLED
from sanic.config import Config
from sanic.exceptions import SanicException
from sanic.response import text
@ -19,15 +20,6 @@ def clear_app_registry():
Sanic._app_registry = {}
def uvloop_installed():
try:
import uvloop # noqa
return True
except ImportError:
return False
def test_app_loop_running(app):
@app.get("/test")
async def handler(request):
@ -39,7 +31,7 @@ def test_app_loop_running(app):
def test_create_asyncio_server(app):
if not uvloop_installed():
if not UVLOOP_INSTALLED:
loop = asyncio.get_event_loop()
asyncio_srv_coro = app.create_server(return_asyncio_server=True)
assert isawaitable(asyncio_srv_coro)
@ -48,7 +40,7 @@ def test_create_asyncio_server(app):
def test_asyncio_server_no_start_serving(app):
if not uvloop_installed():
if not UVLOOP_INSTALLED:
loop = asyncio.get_event_loop()
asyncio_srv_coro = app.create_server(
port=43123,
@ -60,7 +52,7 @@ def test_asyncio_server_no_start_serving(app):
def test_asyncio_server_start_serving(app):
if not uvloop_installed():
if not UVLOOP_INSTALLED:
loop = asyncio.get_event_loop()
asyncio_srv_coro = app.create_server(
port=43124,