Skip to content

Instantly share code, notes, and snippets.

@fictorial
Last active August 29, 2015 14:08
Show Gist options
  • Save fictorial/351ead0c6cf7387b5b41 to your computer and use it in GitHub Desktop.
Save fictorial/351ead0c6cf7387b5b41 to your computer and use it in GitHub Desktop.
autoreload
#!/usr/bin/env python
from os.path import getmtime
import sys, subprocess, time
import signal
usage = 'usage: autoreload "/bin/foo --bar" *.py'
try:
cmd = sys.argv[1]
except IndexError:
print 'nothing to restart'
print usage
sys.exit(1)
files = sys.argv[2:]
if len(files) == 0:
print 'nothing to monitor'
print usage
sys.exit(1)
process = subprocess.Popen(cmd, shell=True)
def interrupt_process(sig, frame):
process.send_signal(signal.SIGINT)
sys.exit(signal.SIGINT)
signal.signal(signal.SIGINT, interrupt_process)
try:
while True:
mtimes = [(f, getmtime(f)) for f in files]
time.sleep(1)
for f, mtime in mtimes:
if getmtime(f) > mtime:
print 'AUTORELOAD: change detected in %s' % f
print 'AUTORELOAD: stopping process "%s"' % cmd
process.kill()
print 'AUTORELOAD: starting process "%s"' % cmd
process = subprocess.Popen(cmd, shell=True)
break
except Exception as e:
print e.message
process.kill()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment