xref: /unit/test/python/lifespan/empty/asgi.py (revision 1628:93e2931202bb)
1import os
2
3
4async def application(scope, receive, send):
5    if scope['type'] == 'lifespan':
6        with open('version', 'w+') as f:
7            f.write(
8                scope['asgi']['version'] + ' ' + scope['asgi']['spec_version']
9            )
10        while True:
11            message = await receive()
12            if message['type'] == 'lifespan.startup':
13                os.remove('startup')
14                await send({'type': 'lifespan.startup.complete'})
15            elif message['type'] == 'lifespan.shutdown':
16                os.remove('shutdown')
17                await send({'type': 'lifespan.shutdown.complete'})
18                return
19
20    if scope['type'] == 'http':
21        await send(
22            {
23                'type': 'http.response.start',
24                'status': 204,
25                'headers': [(b'content-length', b'0'),],
26            }
27        )
28