import asyncio import signal waker = asyncio.Event() @asyncio.coroutine def hello_world(): while True: print("Hello World!") # sleep for a second but wake up immediately upon SIGHUP try: yield from asyncio.wait_for(waker.wait(), timeout=1) print("Sleep was interrupted") except asyncio.TimeoutError: pass def sig_wakeup(): print("Waking") waker.set() waker.clear() print("Woke") loop = asyncio.get_event_loop() loop.add_signal_handler(signal.SIGHUP, sig_wakeup) # Blocking call which returns when the hello_world() coroutine is done try: loop.run_until_complete(hello_world()) finally: # otherwise complaints in remove_signal_handler loop.close()