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