Skip to content

Instantly share code, notes, and snippets.

@socrateslee
Created May 9, 2013 15:36
Show Gist options
  • Save socrateslee/5548239 to your computer and use it in GitHub Desktop.
Save socrateslee/5548239 to your computer and use it in GitHub Desktop.
Inject a signal handler before the existed signal handler.
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)
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