question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Docs] Lifespan ref implementation

See original GitHub issue

Hi everyone. Looking at https://asgi.readthedocs.io/en/latest/specs/lifespan.html , I saw the following example implementation of the lifespan type.

        while True:
            message = await receive()
            if message['type'] == 'lifespan.startup':
                ... # Do some startup here!
                await send({'type': 'lifespan.startup.complete'})
            elif message['type'] == 'lifespan.shutdown':
                ... # Do some shutdown here!
                await send({'type': 'lifespan.shutdown.complete'})
                return

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
andrewgodwincommented, Feb 25, 2021

Oh, you’re right, sorry - the loop should be outside the if statement there. That does need fixing!

0reactions
srkunzecommented, Feb 22, 2021

I get the feeling, we might be talking past each other. This is the complete code example from the docs:

image

As I understand it, the highlighted loop (while True) is just for the lifespan protocol, which only contains two message types to be received and processed in this order.

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object Lifecycle Management | Cloud Storage
Introduction · Downgrade the storage class of objects older than 365 days to Coldline storage. · Delete objects created before January 1, 2019....
Read more >
Validating References with Lifetimes - The Rust Programming ...
Most people use the name 'a for the first lifetime annotation. We place lifetime parameter annotations after the & of a reference, using...
Read more >
Best practices for Cloud Firestore - Firebase
To implement parallel reads as you migrate traffic to a new collection, read from the old collection first. If the document is missing,...
Read more >
Dependency injection guidelines - .NET | Microsoft Learn
The app requires an IDisposable instance with a transient lifetime for either of the following scenarios: ... Use the factory pattern to create...
Read more >
Automatic Reference Counting - Documentation - Swift.org
The instances can then refer to each other without creating a strong reference cycle. Use a weak reference when the other instance has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found