7 Helper to run one or more async functions synchronously, with graceful
8 handling of SIGINT / Ctrl-C.
10 Returns the return value of the last function.
16 run
._sigint
= False # function attr to allow setting from closure
17 loop
= asyncio
.get_event_loop()
25 loop
.add_signal_handler(signal
.SIGINT
, abort
)
27 except (ValueError, OSError, RuntimeError) as e
:
28 # add_signal_handler doesn't work in a thread
29 if 'main thread' not in str(e
):
33 task
= loop
.create_task(step
)
34 loop
.run_until_complete(asyncio
.wait([task
], loop
=loop
))
36 raise KeyboardInterrupt()
38 raise task
.exception()
42 loop
.remove_signal_handler(signal
.SIGINT
)