-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate SIGTERM & SIGINT to supervised processes
Introduces icky global state, but I can't think of another way to do this with signals, since those are inherently global. We exit ourselves after SIGTERM & SIGINT signals have been propagated. Maybe we should wait for our child processes to actually finish?
- Loading branch information
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
""" | ||
asyncio aware version of atexit. | ||
Handles SIGINT and SIGTERM, unlike atexit | ||
""" | ||
import signal | ||
import asyncio | ||
import sys | ||
|
||
_handlers = [] | ||
|
||
signal_handler_set = False | ||
|
||
def add_handler(handler): | ||
if not signal_handler_set: | ||
loop = asyncio.get_event_loop() | ||
loop.add_signal_handler(signal.SIGINT, _handle_signal, signal.SIGINT) | ||
loop.add_signal_handler(signal.SIGTERM, _handle_signal, signal.SIGTERM) | ||
_handlers.append(handler) | ||
|
||
def remove_handler(handler): | ||
_handlers.remove(handler) | ||
|
||
def _handle_signal(signum): | ||
for handler in _handlers: | ||
handler(signum) | ||
sys.exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters