1import os 2 3 4async def handler(prefix, scope, receive, send): 5 if scope['type'] == 'lifespan': 6 with open(f'{prefix}version', 'w+', encoding='utf-8') as f: 7 f.write( 8 f"{scope['asgi']['version']} {scope['asgi']['spec_version']}" 9 ) 10 while True: 11 message = await receive() 12 if message['type'] == 'lifespan.startup': 13 os.remove(f'{prefix}startup') 14 await send({'type': 'lifespan.startup.complete'}) 15 elif message['type'] == 'lifespan.shutdown': 16 os.remove(f'{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': [ 26 (b'content-length', b'0'), 27 ], 28 } 29 ) 30 31 32async def application(scope, receive, send): 33 return await handler('', scope, receive, send) 34 35 36async def application2(scope, receive, send): 37 return await handler('app2_', scope, receive, send) 38