-
Notifications
You must be signed in to change notification settings - Fork 210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Docs] Lifespan ref implementation #241
Comments
That's generally because you are going to be receiving more than just
|
I’m a somewhat new to the world of asyncio, so please bear with me.
The loop from the docs is specifically for
Wouldn’t I then add another receive/send pair in my implementation when that happens? It somehow makes me feel uneasy that this loop+ifs can hide potential bugs quite easily.
Shouldn’t pre-startup not run before startup and startup not before shutdown? I mean I would write my code based on these assumptions (e.g. regarding variable states). |
When you write an app, you have to handle all specs combined, as you have a single main loop - you'd almost never just write a lifespan app, you'd probably combine it with HTTP or something. Thus, you definitely don't know what's coming down the What I'd recommend to avoid bugs in a situation like this is an |
I get the feeling, we might be talking past each other. This is the complete code example from the docs: As I understand it, the highlighted loop ( So, my suggestion is the following: async def app(scope, receive, send):
if scope['type'] == 'lifespan':
message = await receive()
assert message['type'] == 'lifespan.startup'
... # Do some startup here!
await send({'type': 'lifespan.startup.complete'})
message = await receive()
assert message['type'] == 'lifespan.shutdown'
... # Do some shutdown here!
await send({'type': 'lifespan.shutdown.complete'})
else:
pass # Handle other types Am I missing something here? |
Oh, you're right, sorry - the loop should be outside the if statement there. That does need fixing! |
Hi everyone. Looking at https://asgi.readthedocs.io/en/latest/specs/lifespan.html , I saw the following example implementation of the
lifespan
type.My naive implementation would consist of four flat lines of code only (receive, send, receive, send).
What is the reason for the
while True
loop and the fail-safe message handling here? It's seems like I am missing something.The text was updated successfully, but these errors were encountered: