ASGI working

This commit is contained in:
Adam Hopkins 2021-02-08 12:43:10 +02:00
parent 0d5b2a0f69
commit 64f0496d9e
4 changed files with 13 additions and 9 deletions

View File

@ -1037,8 +1037,8 @@ class Sanic(BaseSanic):
details: https://asgi.readthedocs.io/en/latest/""" details: https://asgi.readthedocs.io/en/latest/"""
# raise Exception("call") # raise Exception("call")
self.asgi = True self.asgi = True
self.router.finalize() self._asgi_app = await ASGIApp.create(self, scope, receive, send)
asgi_app = await ASGIApp.create(self, scope, receive, send) asgi_app = self._asgi_app
await asgi_app() await asgi_app()
# _asgi_single_callable = True # We conform to ASGI 3.0 single-callable # _asgi_single_callable = True # We conform to ASGI 3.0 single-callable

View File

@ -131,7 +131,6 @@ class Lifespan:
in sequence since the ASGI lifespan protocol only supports a single in sequence since the ASGI lifespan protocol only supports a single
startup event. startup event.
""" """
print(">>> starting up")
self.asgi_app.sanic_app.router.finalize() self.asgi_app.sanic_app.router.finalize()
listeners = self.asgi_app.sanic_app.listeners.get( listeners = self.asgi_app.sanic_app.listeners.get(
"before_server_start", [] "before_server_start", []
@ -192,7 +191,6 @@ class ASGIApp:
async def create( async def create(
cls, sanic_app, scope: ASGIScope, receive: ASGIReceive, send: ASGISend cls, sanic_app, scope: ASGIScope, receive: ASGIReceive, send: ASGISend
) -> "ASGIApp": ) -> "ASGIApp":
raise Exception("create")
instance = cls() instance = cls()
instance.sanic_app = sanic_app instance.sanic_app = sanic_app
instance.transport = MockTransport(scope, receive, send) instance.transport = MockTransport(scope, receive, send)
@ -206,7 +204,6 @@ class ASGIApp:
] ]
) )
instance.lifespan = Lifespan(instance) instance.lifespan = Lifespan(instance)
print(instance.lifespan)
if scope["type"] == "lifespan": if scope["type"] == "lifespan":
await instance.lifespan(scope, receive, send) await instance.lifespan(scope, receive, send)
@ -296,5 +293,4 @@ class ASGIApp:
""" """
Handle the incoming request. Handle the incoming request.
""" """
print("......")
await self.sanic_app.handle_request(self.request) await self.sanic_app.handle_request(self.request)

View File

@ -137,6 +137,7 @@ class GunicornWorker(base.Worker):
await _shutdown await _shutdown
async def _run(self): async def _run(self):
self.app.router.finalize()
for sock in self.sockets: for sock in self.sockets:
state = dict(requests_count=0) state = dict(requests_count=0)
self._server_settings["host"] = None self._server_settings["host"] = None

View File

@ -54,7 +54,6 @@ def test_listeners_triggered():
@app.listener("before_server_start") @app.listener("before_server_start")
def do_before_server_start(*args, **kwargs): def do_before_server_start(*args, **kwargs):
raise Exception("......")
nonlocal before_server_start nonlocal before_server_start
before_server_start = True before_server_start = True
@ -73,6 +72,10 @@ def test_listeners_triggered():
nonlocal after_server_stop nonlocal after_server_stop
after_server_stop = True after_server_stop = True
@app.route("/")
def handler(request):
return text("...")
class CustomServer(uvicorn.Server): class CustomServer(uvicorn.Server):
def install_signal_handlers(self): def install_signal_handlers(self):
pass pass
@ -80,7 +83,7 @@ def test_listeners_triggered():
config = uvicorn.Config(app=app, loop="asyncio", limit_max_requests=0) config = uvicorn.Config(app=app, loop="asyncio", limit_max_requests=0)
server = CustomServer(config=config) server = CustomServer(config=config)
# with pytest.warns(UserWarning): with pytest.warns(UserWarning):
server.run() server.run()
all_tasks = ( all_tasks = (
@ -123,6 +126,10 @@ def test_listeners_triggered_async(app):
nonlocal after_server_stop nonlocal after_server_stop
after_server_stop = True after_server_stop = True
@app.route("/")
def handler(request):
return text("...")
class CustomServer(uvicorn.Server): class CustomServer(uvicorn.Server):
def install_signal_handlers(self): def install_signal_handlers(self):
pass pass