Created
May 12, 2015 23:43
-
-
Save lukedeo/4b872c7a2d5bd1c5d951 to your computer and use it in GitHub Desktop.
Context based SIGINT 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
class TrainingHandler(object): | |
def __init__(self, sig=signal.SIGINT): | |
self.sig = sig | |
def __enter__(self): | |
self.interrupted = False | |
self.released = False | |
self.original_handler = signal.getsignal(self.sig) | |
def handler(signum, frame): | |
self.release() | |
self.interrupted = True | |
signal.signal(self.sig, handler) | |
return self | |
def __exit__(self, type, value, tb): | |
self.release() | |
def release(self): | |
if self.released: | |
return False | |
signal.signal(self.sig, self.original_handler) | |
self.released = True | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment