import os
import sys
import time
import signal
import threading
import atexit
import Queue

_interval = 1.0
_times = {}
_files = []

_running = False
_queue = Queue.Queue()
_lock = threading.Lock()

def _restart(path):
    _queue.put(True)
    prefix = 'monitor (pid=%d):' % os.getpid()
    print >> sys.stderr, '%s Change detected to \'%s\'.' % (prefix, path)
    print >> sys.stderr, '%s Triggering process restart.' % prefix
    os.kill(os.getpid(), signal.SIGINT)

def _modified(path):
    try:
        # If path doesn't denote a file and were previously
        # tracking it, then it has been removed or the file type
        # has changed so force a restart. If not previously
        # tracking the file then we can ignore it as probably
        # pseudo reference such as when file extracted from a
        # collection of modules contained in a zip file.

        if not os.path.isfile(path):
            return path in _times

        # Check for when file last modified.

        mtime = os.stat(path).st_mtime
        if path not in _times:
            _times[path] = mtime

        # Force restart when modification time has changed, even
        # if time now older, as that could indicate older file
        # has been restored.

        if mtime != _times[path]:
            return True
    except:
        # If any exception occured, likely that file has been
        # been removed just before stat(), so force a restart.

        return True

    return False

def _monitor():
    while 1:
        # Check modification times on all files in sys.modules.

        for module in sys.modules.values():
            if not hasattr(module, '__file__'):
                continue
            path = getattr(module, '__file__')
            if not path:
                continue
            if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
                path = path[:-1]
            if _modified(path):
                return _restart(path)

        # Check modification times on files which have
        # specifically been registered for monitoring.

        for path in _files:
            if _modified(path):
                return _restart(path)

        # Go to sleep for specified interval.

        try:
            return _queue.get(timeout=_interval)
        except:
            pass

_thread = threading.Thread(target=_monitor)
_thread.setDaemon(True)

def _exiting():
    try:
        _queue.put(True)
    except:
        pass
    _thread.join()

atexit.register(_exiting)

def track(path):
    if not path in _files:
        _files.append(path)

def start(interval=1.0):
    global _interval
    if interval < _interval:
        _interval = interval

    global _running
    _lock.acquire()
    if not _running:
        prefix = 'monitor (pid=%d):' % os.getpid()
        print >> sys.stderr, '%s Starting change monitor.' % prefix
        _running = True
        _thread.start()
    _lock.release()

@@ -1,12 +1,51 @@
+from __future__ import print_function%0A%0A
 import os%0Aim
@@ -117,17 +117,17 @@
 %0Aimport 
-Q
+q
 ueue%0A%0A_i
@@ -191,17 +191,17 @@
 queue = 
-Q
+q
 ueue.Que
@@ -328,32 +328,17 @@
    print
- %3E%3E sys.stderr, 
+(
 '%25s Chan
@@ -381,34 +381,37 @@
 ath)
-%0A    print %3E%3E sys.stderr, 
+, file=sys.stderr)%0A    print(
 '%25s 
@@ -438,25 +438,34 @@
 estart.'
- %25 prefix
+, file=sys.stderr)
 %0A    os.
@@ -1622,24 +1622,29 @@
 r module in 
+list(
 sys.modules.
@@ -1651,16 +1651,17 @@
 values()
+)
 :%0A      
@@ -2849,24 +2849,9 @@
 rint
- %3E%3E sys.stderr, 
+(
 '%25s 
@@ -2884,16 +2884,34 @@
 %25 prefix
+, file=sys.stderr)
 %0A       

Fix python3 in monitor.py