From 64f0496d9e43a2af667a8f6e9cc9b7a1fa5fb3c7 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Mon, 8 Feb 2021 12:43:10 +0200 Subject: [PATCH] ASGI working --- sanic/app.py | 4 ++-- sanic/asgi.py | 4 ---- sanic/worker.py | 1 + tests/test_asgi.py | 13 ++++++++++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 45e29ab4..ca8c6936 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1037,8 +1037,8 @@ class Sanic(BaseSanic): details: https://asgi.readthedocs.io/en/latest/""" # raise Exception("call") self.asgi = True - self.router.finalize() - asgi_app = await ASGIApp.create(self, scope, receive, send) + self._asgi_app = await ASGIApp.create(self, scope, receive, send) + asgi_app = self._asgi_app await asgi_app() # _asgi_single_callable = True # We conform to ASGI 3.0 single-callable diff --git a/sanic/asgi.py b/sanic/asgi.py index 8952f88f..73b2c99e 100644 --- a/sanic/asgi.py +++ b/sanic/asgi.py @@ -131,7 +131,6 @@ class Lifespan: in sequence since the ASGI lifespan protocol only supports a single startup event. """ - print(">>> starting up") self.asgi_app.sanic_app.router.finalize() listeners = self.asgi_app.sanic_app.listeners.get( "before_server_start", [] @@ -192,7 +191,6 @@ class ASGIApp: async def create( cls, sanic_app, scope: ASGIScope, receive: ASGIReceive, send: ASGISend ) -> "ASGIApp": - raise Exception("create") instance = cls() instance.sanic_app = sanic_app instance.transport = MockTransport(scope, receive, send) @@ -206,7 +204,6 @@ class ASGIApp: ] ) instance.lifespan = Lifespan(instance) - print(instance.lifespan) if scope["type"] == "lifespan": await instance.lifespan(scope, receive, send) @@ -296,5 +293,4 @@ class ASGIApp: """ Handle the incoming request. """ - print("......") await self.sanic_app.handle_request(self.request) diff --git a/sanic/worker.py b/sanic/worker.py index 765f26f7..6cb7e180 100644 --- a/sanic/worker.py +++ b/sanic/worker.py @@ -137,6 +137,7 @@ class GunicornWorker(base.Worker): await _shutdown async def _run(self): + self.app.router.finalize() for sock in self.sockets: state = dict(requests_count=0) self._server_settings["host"] = None diff --git a/tests/test_asgi.py b/tests/test_asgi.py index dc80048d..d5111c87 100644 --- a/tests/test_asgi.py +++ b/tests/test_asgi.py @@ -54,7 +54,6 @@ def test_listeners_triggered(): @app.listener("before_server_start") def do_before_server_start(*args, **kwargs): - raise Exception("......") nonlocal before_server_start before_server_start = True @@ -73,6 +72,10 @@ def test_listeners_triggered(): nonlocal after_server_stop after_server_stop = True + @app.route("/") + def handler(request): + return text("...") + class CustomServer(uvicorn.Server): def install_signal_handlers(self): pass @@ -80,8 +83,8 @@ def test_listeners_triggered(): config = uvicorn.Config(app=app, loop="asyncio", limit_max_requests=0) server = CustomServer(config=config) - # with pytest.warns(UserWarning): - server.run() + with pytest.warns(UserWarning): + server.run() all_tasks = ( asyncio.Task.all_tasks() @@ -123,6 +126,10 @@ def test_listeners_triggered_async(app): nonlocal after_server_stop after_server_stop = True + @app.route("/") + def handler(request): + return text("...") + class CustomServer(uvicorn.Server): def install_signal_handlers(self): pass