Skip to content

Commit

Permalink
Fix reloader issues
Browse files Browse the repository at this point in the history
Fix #621
  • Loading branch information
untitaker committed Jan 25, 2015
1 parent 56d1127 commit 4e2c674
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ deps=
pytest-xprocess
redis
requests
watchdog==0.8.1
watchdog

# Python 2
py26: python-memcached
Expand Down
58 changes: 31 additions & 27 deletions werkzeug/_reloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,38 +200,42 @@ def on_modified(self, event):

self.observer_class = Observer
self.event_handler = _CustomHandler()
self.should_reload = False

def trigger_reload(self, filename):
# This is called inside an event handler, which means we can't throw
# SystemExit here. https://github.com/gorakhargosh/watchdog/issues/294
self.should_reload = True
ReloaderLoop.trigger_reload(self, filename)

def run(self):
watches = {}
observer = self.observer_class()
observer.start()

while not self.should_reload:
to_delete = set(watches)
paths = _find_common_roots(
_find_observable_paths(self.extra_files))
for path in paths:
if path not in watches:
try:
watches[path] = observer.schedule(
self.event_handler, path, recursive=True)
except OSError:
# "Path is not a directory". We could filter out
# those paths beforehand, but that would cause
# additional stat calls.
watches[path] = None
to_delete.discard(path)
for path in to_delete:
watch = watches.pop(path, None)
if watch is not None:
observer.unschedule(watch)
self.observable_paths = paths
self._sleep(self.interval)

def monitor_update_thread():
while 1:
to_delete = set(watches)
paths = _find_common_roots(
_find_observable_paths(self.extra_files))
for path in paths:
if path not in watches:
try:
watches[path] = observer.schedule(
self.event_handler, path, recursive=True)
except OSError:
# "Path is not a directory". We could filter out
# those paths beforehand, but that would cause
# additional stat calls.
watches[path] = None
to_delete.discard(path)
for path in to_delete:
watch = watches.pop(path, None)
if watch is not None:
observer.unschedule(watch)
self.observable_paths = paths
self._sleep(self.interval)

t = threading.Thread(target=monitor_update_thread, args=())
t.setDaemon(True)
t.start()
observer.run()
sys.exit(3)


reloader_loops = {
Expand Down

0 comments on commit 4e2c674

Please sign in to comment.