From b0a66f3502b7959aed19c25dad1f2deb86f7208d Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sun, 25 Nov 2018 09:29:47 +0100 Subject: single: Graceful ^C Allow cancellation of timeout wait. --- crocoite/cli.py | 6 +++++- crocoite/controller.py | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'crocoite') diff --git a/crocoite/cli.py b/crocoite/cli.py index e4a46ee..390cae6 100644 --- a/crocoite/cli.py +++ b/crocoite/cli.py @@ -69,7 +69,11 @@ def single (): service=service, handler=handler, behavior=b, logger=logger) try: loop = asyncio.get_event_loop() - loop.run_until_complete(controller.run ()) + run = asyncio.ensure_future (controller.run ()) + stop = lambda signum: run.cancel () + loop.add_signal_handler (signal.SIGINT, stop, signal.SIGINT) + loop.add_signal_handler (signal.SIGTERM, stop, signal.SIGTERM) + loop.run_until_complete(run) loop.close() ret = SingleExitStatus.Ok except Crashed: diff --git a/crocoite/controller.py b/crocoite/controller.py index 4d95b09..5f4fe10 100644 --- a/crocoite/controller.py +++ b/crocoite/controller.py @@ -178,7 +178,14 @@ class SinglePageController: idleTimeout = None while True: idleProc = asyncio.ensure_future (l.idle.wait ()) - finished, pending = await asyncio.wait([idleProc, timeoutProc], return_when=asyncio.FIRST_COMPLETED, timeout=idleTimeout) + try: + finished, pending = await asyncio.wait([idleProc, timeoutProc], + return_when=asyncio.FIRST_COMPLETED, timeout=idleTimeout) + except asyncio.CancelledError: + idleProc.cancel () + timeoutProc.cancel () + break + if not finished: # idle timeout idleProc.cancel () -- cgit v1.2.3