From 877f5e6aefca221b43cec94862e240d38e1ff2f5 Mon Sep 17 00:00:00 2001 From: prryplatypus <25409753+prryplatypus@users.noreply.github.com> Date: Mon, 6 Dec 2021 20:35:11 +0100 Subject: [PATCH] Only change loop policy in non-async methods --- sanic/app.py | 16 +++++----------- tests/test_app.py | 8 +++++--- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index da54d5dd..bb27d47a 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1102,6 +1102,9 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): register_sys_signals=register_sys_signals, ) + if self.config.USE_UVLOOP: + use_uvloop() + try: self.is_running = True self.is_stopping = False @@ -1213,7 +1216,7 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): ssl=ssl, sock=sock, unix=unix, - loop=True, + loop=get_event_loop(), protocol=protocol, backlog=backlog, run_async=return_asyncio_server, @@ -1323,7 +1326,7 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): sock: Optional[socket] = None, unix: Optional[str] = None, workers: int = 1, - loop: Union[AbstractEventLoop, bool] = None, + loop: AbstractEventLoop = None, protocol: Type[Protocol] = HttpProtocol, backlog: int = 100, register_sys_signals: bool = True, @@ -1356,13 +1359,6 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): display_host = f"[{host}]" if ":" in host else host serve_location = f"{proto}://{display_host}:{port}" - if not isinstance(loop, AbstractEventLoop): - if self.config.USE_UVLOOP: - use_uvloop() - - if loop is True: - loop = get_event_loop() - ssl = process_to_context(ssl) server_settings = { @@ -1459,8 +1455,6 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta): details: https://asgi.readthedocs.io/en/latest """ self.asgi = True - if self.config.USE_UVLOOP: - use_uvloop() self.motd("") self._asgi_app = await ASGIApp.create(self, scope, receive, send) asgi_app = self._asgi_app diff --git a/tests/test_app.py b/tests/test_app.py index 35412d38..ba244501 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -440,6 +440,7 @@ def test_custom_context(): assert app.ctx == ctx + def test_uvloop_usage(app, monkeypatch): @app.get("/test") def handler(request): @@ -457,7 +458,7 @@ def test_uvloop_usage(app, monkeypatch): use_uvloop.assert_called_once() -def test_uvloop_usage_with_create_server(app, monkeypatch): +def test_uvloop_is_ignored_with_create_server(app, monkeypatch): @app.get("/test") def handler(request): return text("ok") @@ -481,9 +482,10 @@ def test_uvloop_usage_with_create_server(app, monkeypatch): asyncio_server_kwargs=dict(start_serving=False) ) loop.run_until_complete(asyncio_srv_coro) - use_uvloop.assert_called_once() + use_uvloop.assert_not_called() + def test_cannot_run_fast_and_workers(app): message = "You cannot use both fast=True and workers=X" with pytest.raises(RuntimeError, match=message): - app.run(fast=True, workers=4) \ No newline at end of file + app.run(fast=True, workers=4)