Created
May 9, 2013 15:36
-
-
Save socrateslee/5548239 to your computer and use it in GitHub Desktop.
Inject a signal handler before the existed signal handler.
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
import signal | |
import time | |
from signal_inject import * | |
def dummy_handler(signum, frame): | |
print "In dummy handler" | |
inject_signal_handler(signal.SIGINT, dummy_handler) | |
while 1: | |
time.sleep(1) |
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
import signal | |
def inject_signal_handler(signum, handler): | |
''' | |
Execute the handler before the existed handler. | |
''' | |
# Get the old handler | |
old_handler = signal.getsignal(signum) | |
# Create a new combined handler | |
def new_handler(*sub): | |
handler(*sub) | |
if callable(old_handler): | |
old_handler(*sub) | |
# Re-bind the handler | |
signal.signal(signum, new_handler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment