# File: asyncio_signal.py
#
# Example showing how Unix system event notifications are handled by `asyncio`.
# Signal handler callbacks are interleaved with the coroutines. Callbacks are
# managed by the event loop.
import asyncio
import functools
import os
import signal

def signal_handler(name):
    print('signal_handler({!r})'.format(name))

event_loop = asyncio.get_event_loop()
event_loop.add_signal_handler(
    signal.SIGHUP, functools.partial(signal_handler, name='SIGHUP'),
)

event_loop.add_signal_handler(
    signal.SIGUSR1, functools.partial(signal_handler, name='SIGUSR1')
)

event_loop.add_signal_handler(
    signal.SIGINT, functools.partial(signal_handler, name='SIGINT')
)


async def send_signals():
    pid = os.getpid()
    print('starting send_signals for {}'.format(pid))
    for name in ['SIGHUP', 'SIGHUP', 'SIGUSR1', 'SIGINT']:
        print('sending {}'.format(name))
        os.kill(pid, getattr(signal, name))
    # Yield control to allow the signal handler to run, since the signal does
    # not intterrupt the program flow otherwise
        print('yielding control')
        await asyncio.sleep(0.01)
    return


try:
    event_loop.run_until_complete(send_signals())
finally:
    event_loop.close()
