From cc03611bcbd7d167f2c6b3617bf1bbece3f69b71 Mon Sep 17 00:00:00 2001 From: Guilhem Saurel Date: Sun, 8 Mar 2020 15:54:50 +0100 Subject: [PATCH] cleaner aiohttp use Thanks a lot @djanos ! --- matrix_webhook.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/matrix_webhook.py b/matrix_webhook.py index 3780b28..ce02bae 100755 --- a/matrix_webhook.py +++ b/matrix_webhook.py @@ -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()