Skip to content

Instantly share code, notes, and snippets.

@rjpower
Created June 15, 2013 17:45
Show Gist options
  • Save rjpower/5788911 to your computer and use it in GitHub Desktop.
Save rjpower/5788911 to your computer and use it in GitHub Desktop.
File based watchdog timer.
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