<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Django foo &#187; autoreloadmodwsgi</title>
	<atom:link href="http://www.djangofoo.com/tag/autoreloadmodwsgi/feed" rel="self" type="application/rss+xml" />
	<link>http://www.djangofoo.com</link>
	<description>Django Tips and Tweaks</description>
	<lastBuildDate>Wed, 18 Aug 2010 09:38:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Autoreload/Restart modwsgi on source change</title>
		<link>http://www.djangofoo.com/320/autoreloadrestart-modwsgi-on-source-change</link>
		<comments>http://www.djangofoo.com/320/autoreloadrestart-modwsgi-on-source-change#comments</comments>
		<pubDate>Sun, 20 Jun 2010 10:50:40 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[autoreloadmodwsgi]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=320</guid>
		<description><![CDATA[When developing with Django&#8217;s DEV server it has a very nice feature, the auto-reload. That is, every time you make changes to a file, the server is automatically restarted. We can have the same with Apache/modwsgi, all you need to do is the following: pre requirements: Linux, python, apache, modwsgi running with WSGIDaemonProcess 1) Find [...]]]></description>
			<content:encoded><![CDATA[<p>When developing with  Django&#8217;s DEV server it has a very nice feature, the auto-reload.<br />
That is, every time you make changes to a file, the server is automatically restarted.  </p>
<p>We can have the same with Apache/modwsgi, all you need to do is the following:<br />
<strong>pre requirements</strong>: Linux, python, apache, modwsgi running with WSGIDaemonProcess</p>
<p>1) Find the <strong>site-packages</strong> of your current python </p>
<pre class="brush:python">
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
</pre>
<p>2) <strong>monitor.py</strong> , copy this file inside your site-packages directory</p>
<pre class="brush:python">
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()
</pre>
<p>3) Finally edit your application WSGI file and add the following <strong>before your application=django.core.handlers.wsgi.WSGIHandler()</strong>:</p>
<pre class="brush:python">
...
import monitor

monitor.start(interval=5.0) # check every 5 seconds

for root,dirs,files in os.walk('/some/folder/where/your/app/is'):
        for x in files:
            if os.path.splitext(x)[1].lower() == '.py': # only .py files please.
                monitor.track(root + x)
...
</pre>
<p>original code is at <a href="http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Monitoring_For_Code_Changes" target="_new">http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Monitoring_For_Code_Changes</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/320/autoreloadrestart-modwsgi-on-source-change/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

