Created
November 25, 2014 02:06
-
-
Save brent-hoover/f6143d7bad31608561fb to your computer and use it in GitHub Desktop.
The Watcher that will not die
This file contains hidden or 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
| #!/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