Created
June 15, 2013 17:45
-
-
Save rjpower/5788911 to your computer and use it in GitHub Desktop.
File based watchdog timer.
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 FileWatchdog(threading.Thread): | |
"""Watchdog for a file (typically `sys.stdin`). | |
When the file closes, terminate the process. | |
(This typically occurs when the parent process is lost.) | |
""" | |
def __init__(self, file_handle): | |
threading.Thread.__init__(self, name='WatchdogThread') | |
self.setDaemon(True) | |
self.file_handle = file_handle | |
# self.log = open('/tmp/watchdog.%d' % os.getpid(), 'w') | |
def run(self): | |
f = [self.file_handle] | |
while 1: | |
r, w, x = select.select(f, f, f, 1.0) | |
# print >>self.log, 'Watchdog running: %s %s %s' % (r,w,x) | |
# self.log.flush() | |
if r: | |
# print >>self.log, 'Watchdog: file closed. Shutting down.' | |
# self.log.flush() | |
os._exit(1) | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment