cleaner aiohttp use

Thanks a lot @djanos !
This commit is contained in:
Guilhem Saurel 2020-03-08 15:54:50 +01:00
parent 2c97fa3d3f
commit cc03611bcb

View file

@ -9,6 +9,7 @@ v2: matrix-nio & aiohttp
import asyncio
import json
import os
from signal import SIGINT, SIGTERM
from aiohttp import web
from nio import AsyncClient
@ -46,7 +47,7 @@ async def handler(request):
status=status)
async def main():
async def main(event):
"""
Main coroutine
@ -61,16 +62,27 @@ async def main():
site = web.TCPSite(runner, *SERVER_ADDRESS)
await site.start()
# pause here for very long time by serving HTTP requests and
# waiting for keyboard interruption
await asyncio.sleep(100 * 3600)
# Run until we get a shutdown request
await event.wait()
# Cleanup
await runner.cleanup()
await CLIENT.close()
def terminate(event, signal):
event.set()
loop = asyncio.get_event_loop()
loop.remove_signal_handler(signal)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
event = asyncio.Event()
for sig in (SIGINT, SIGTERM):
loop.add_signal_handler(sig, terminate, event, sig)
loop.run_until_complete(main(event))
try:
loop.run_until_complete(main())
except KeyboardInterrupt:
pass
loop.close()