forked from celery/celery
/
autoreload.py
179 lines (144 loc) · 5.09 KB
/
autoreload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
"""
celery.worker.autoreload
~~~~~~~~~~~~~~~~~~~~~~~~
This module implements automatic module reloading
"""
from __future__ import absolute_import
from __future__ import with_statement
import os
import sys
import time
import select
import hashlib
from collections import defaultdict
from .. import current_app
def file_hash(filename, algorithm='md5'):
hobj = hashlib.new(algorithm)
with open(filename, 'rb') as f:
for chunk in iter(lambda: f.read(2 ** 20), ''):
hobj.update(chunk)
return hobj.digest()
class StatMonitor(object):
"""File change monitor based on `stat` system call"""
def __init__(self, files, on_change=None, interval=0.5):
self._files = files
self._interval = interval
self._on_change = on_change
self._modify_times = defaultdict(int)
def start(self):
while True:
modified = {}
for m in self._files:
mt = self._mtime(m)
if mt is None:
break
if self._modify_times[m] != mt:
modified[m] = mt
else:
if modified:
self.on_change(modified.keys())
self._modify_times.update(modified)
time.sleep(self._interval)
def on_change(self, modified):
if self._on_change:
return self._on_change(modified)
@classmethod
def _mtime(cls, path):
try:
return os.stat(path).st_mtime
except:
return
class KQueueMonitor(object):
"""File change monitor based on BSD kernel event notifications"""
def __init__(self, files, on_change=None):
assert hasattr(select, 'kqueue')
self._files = dict([(f, None) for f in files])
self._on_change = on_change
def start(self):
try:
self._kq = select.kqueue()
kevents = []
for f in self._files:
self._files[f] = fd = os.open(f, os.O_RDONLY)
ev = select.kevent(fd,
filter=select.KQ_FILTER_VNODE,
flags=select.KQ_EV_ADD |
select.KQ_EV_ENABLE |
select.KQ_EV_CLEAR,
fflags=select.KQ_NOTE_WRITE |
select.KQ_NOTE_EXTEND)
kevents.append(ev)
events = self._kq.control(kevents, 0)
while True:
events = self._kq.control(kevents, 1)
fds = [e.ident for e in events]
modified = [k for k, v in self._files.iteritems()
if v in fds]
self.on_change(modified)
finally:
self.close()
def close(self):
self._kq.close()
for f in self._files:
if self._files[f] is not None:
os.close(self._files[f])
self._files[f] = None
def on_change(self, modified):
if self._on_change:
return self._on_change(modified)
try:
import pyinotify
except ImportError:
pyinotify = None # noqa
class InotifyMonitor(pyinotify and pyinotify.ProcessEvent or object):
"""File change monitor based on Linux kernel `inotify` subsystem"""
def __init__(self, modules, on_change=None):
assert pyinotify
self._modules = modules
self._on_change = on_change
def start(self):
try:
self._wm = pyinotify.WatchManager()
self._notifier = pyinotify.Notifier(self._wm)
for m in self._modules:
self._wm.add_watch(m, pyinotify.IN_MODIFY)
self._notifier.loop()
finally:
self.close()
def close(self):
self._notifier.stop()
self._wm.close()
def process_IN_MODIFY(self, event):
self.on_change(event.pathname)
def on_change(self, modified):
if self._on_change:
self._on_change(modified)
if hasattr(select, 'kqueue'):
_monitor_cls = KQueueMonitor
elif sys.platform.startswith('linux') and pyinotify:
_monitor_cls = InotifyMonitor
else:
_monitor_cls = StatMonitor
class AutoReloader(object):
"""Tracks changes in modules and fires reload commands"""
def __init__(self, modules, monitor_cls=_monitor_cls, *args, **kwargs):
self._monitor = monitor_cls(modules, self.on_change, *args, **kwargs)
self._hashes = dict([(f, file_hash(f)) for f in modules])
def start(self):
self._monitor.start()
def on_change(self, files):
modified = []
for f in files:
fhash = file_hash(f)
if fhash != self._hashes[f]:
modified.append(f)
self._hashes[f] = fhash
if modified:
self._reload(map(self._module_name, modified))
def _reload(self, modules):
current_app.control.broadcast("pool_restart",
arguments={"imports": modules, "reload_modules": True})
@classmethod
def _module_name(cls, path):
return os.path.splitext(os.path.basename(path))[0]