xref: /unit/test/python/lifespan/empty/asgi.py (revision 1873:d0cc4fd78d43)
1import os
2
3
4async def handler(prefix, scope, receive, send):
5    if scope['type'] == 'lifespan':
6        with open(prefix + '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(prefix + 'startup')
14                await send({'type': 'lifespan.startup.complete'})
15            elif message['type'] == 'lifespan.shutdown':
16                os.remove(prefix + '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
29
30async def application(scope, receive, send):
31    return await handler('', scope, receive, send)
32
33
34async def application2(scope, receive, send):
35    return await handler('app2_', scope, receive, send)
36