Professional Documents
Culture Documents
Pywatch Copy 7
Pywatch Copy 7
/usr/bin/python
# Example:
# Throw this script in the home directory of your
# ERPNext server and run
#./pywatch frappe-bench/apps/ &
# Explanation:
# While testing code on a Frappe Production setup, modifications
# of python code inside an app don't take effect until you run
# "bench restart", which usually takes around 10-13 seconds.
# We discovered that by killing gunicorn (pkill gunicord), the
# python code gets reloaded and gunicorn process gets
# restarted almost instantly.
#
# This script watches for changes in python files and
# automatically triggers gunicord restart whenever a
# python file changes. This speeds up development
# process and makes it possible to modify python code
# in production without 13 second downtime caused by
# running "bench restart".
# TODO:
# -possibly exclude .git directory with exclude_filter
# -add a timer and don't kill gunicorn more than once every second.
# -Issue: when modifying python with vim, on_event is called twice.
# -Check what happens when a bunch of python files get changed at the same time,
for example when installing new app.
debug = False
if len(sys.argv) != 2:
if len(sys.argv) == 3 and sys.argv[2] == '-d':
debug = True
else:
print('%s <PATH_TO_MONITOR> [-d]' % sys.argv[0])
exit()
path = sys.argv[1]
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm)
def on_event(arg):
name = arg.pathname
#print('event %s' % name)
if name.split('.')[-1] == 'py':
if debug:
print('python file changed: %s' % name)
subprocess.call(['pkill', 'gunicorn'])
#watch_mask = pyinotify.ALL_EVENTS
watch_mask = pyinotify.IN_CREATE | pyinotify.IN_MODIFY
#exclude_filter=not_py_file (this can be used to exclude .git directory)
wm.add_watch(path, watch_mask, proc_fun=on_event, rec=True, auto_add=True,
quiet=False)
notifier.loop()