simplify example implementation of the lifespan protocol#242
Conversation
|
As far as I'm aware, this example would be insufficient, since unlike every other ASGI protocol, lifespan handlers are forbidden from throwing in their normal execution flow:
If your application does support Also, ASGI apps works by waiting for the server to signal them (via I believe it should look more like this: async def app(scope, receive, send):
while True:
message = await receive()
if scope['type'] == 'lifespan':
# All exceptions must be caught while handling a lifespan event if lifespan is supported.
if message['type'] == 'lifespan.startup':
try:
... # Do some startup here!
await send({'type': 'lifespan.startup.complete'})
except Exception as e:
await send({'type': 'lifespan.startup.failed', 'message': str(e)})
elif message['type'] == 'lifespan.shutdown':
try:
... # Do some shutdown here!
await send({'type': 'lifespan.shutdown.complete'})
except Exception as e:
await send({'type': 'lifespan.shutdown.failed', 'message': str(e)})
else:
# Invalid or unsupported type, applications should raise an exception to the server.
raise NotImplementedError()
elif scope['type'] == 'http':
... # Handle http |
Should fix #241 (at least from my beginner's perspective)
I leave it here for discussion.