Thanks "Folks" in special Aymeric

Let's go, let me explain about this "".

When users deploy django app in production, the changes in any python 
script need be "reloaded" for apply changes, in this you have two options 
to do this:

1 - restart apache or nginx server
2 - "touch" (command line linux)

Anyway this is a problem, this file can be included in django core for 
apply changes in python script and don't need be reload server. When you 
start a django app

typing: "python runserver".

An internal montior is initialized for apply changes runtime. Would be good 
have this option in production too.

Attachment: sample and in usage.


Em quinta-feira, 13 de outubro de 2016 22:39:31 UTC-3, Ricardo Prado 
> Hy Guys!
> A always use to developer my django apps, but this file ins't 
> included in django current version. In future this can be included for dev 
> tools.
> Thanks.

You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit
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):
    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):
        # 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
        # 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__'):
            path = getattr(module, '__file__')
            if not path:
            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.

            return _queue.get(timeout=_interval)

_thread = threading.Thread(target=_monitor)

def _exiting():


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

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

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

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "framework.settings")

application = get_wsgi_application()

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "framework.settings")

application = get_wsgi_application()

from framework import settings

if settings.DEBUG:
	import framework.monitor

Reply via email to