cleaner aiohttp use
Thanks a lot @djanos !
This commit is contained in:
parent
2c97fa3d3f
commit
cc03611bcb
1 changed files with 20 additions and 8 deletions
|
@ -9,6 +9,7 @@ v2: matrix-nio & aiohttp
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
from signal import SIGINT, SIGTERM
|
||||||
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
from nio import AsyncClient
|
from nio import AsyncClient
|
||||||
|
@ -46,7 +47,7 @@ async def handler(request):
|
||||||
status=status)
|
status=status)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main(event):
|
||||||
"""
|
"""
|
||||||
Main coroutine
|
Main coroutine
|
||||||
|
|
||||||
|
@ -61,16 +62,27 @@ async def main():
|
||||||
site = web.TCPSite(runner, *SERVER_ADDRESS)
|
site = web.TCPSite(runner, *SERVER_ADDRESS)
|
||||||
await site.start()
|
await site.start()
|
||||||
|
|
||||||
# pause here for very long time by serving HTTP requests and
|
# Run until we get a shutdown request
|
||||||
# waiting for keyboard interruption
|
await event.wait()
|
||||||
await asyncio.sleep(100 * 3600)
|
|
||||||
|
# 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__':
|
if __name__ == '__main__':
|
||||||
loop = asyncio.get_event_loop()
|
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()
|
loop.close()
|
||||||
|
|
Loading…
Reference in a new issue