Skip to content

Instantly share code, notes, and snippets.

@brent-hoover
Created November 25, 2014 02:06
Show Gist options
  • Select an option

  • Save brent-hoover/f6143d7bad31608561fb to your computer and use it in GitHub Desktop.

Select an option

Save brent-hoover/f6143d7bad31608561fb to your computer and use it in GitHub Desktop.
The Watcher that will not die
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import threading
import sys
import time
class Watcher(object):
def __init__(self):
self.file = '/Users/brenthoover/Desktop/stub'
self.mtime = os.stat(self.file).st_mtime
self.monitor_count = 0
def start(self):
try:
self._monitor_thread = threading.Thread(target=self.monitor_till_stopped)
self._monitor_thread.start()
except:
print('keyboard interrupt')
sys.exit()
def execute(self):
print('run the application')
def monitor_file(self):
print('monitor: %s' % self.monitor_count)
mtime = os.stat(self.file).st_mtime
if mtime > self.mtime:
print('changed....')
self.execute()
self.mtime = mtime
def monitor_till_stopped(self):
while True:
self.monitor_count += 1
if self.monitor_count > 15:
sys.exit()
self.monitor_file()
time.sleep(1)
if __name__ == '__main__':
w = Watcher()
w.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment