WSGI process reload modules

2019-08-11 19:45发布

问题:

I'm trying to trigger a reload of my WSGI process when any file changes in the folder where it and all it's dependent modules are located.

I've read http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode and I thought I understood it, but this intermittent staleness makes me doubt myself. I'm running in daemon mode like this:

DocumentRoot /usr/local/www/mysite.com/public_html

WSGIScriptAlias /api /usr/local/www/mysite.com/server/server.py
WSGIPassAuthorization On
WSGIDaemonProcess mysite.com threads=15 python-path=/usr/local/www/mysite.com/server
WSGIProcessGroup mysite.com

server.py is the main WSGI application file and all the modules it imports (which are likely to change) are in the same folder as it.

This is what I've come up with and it seems to work most of the time but occasionally I get stuck modules (where I make a change to a source file and the process restarts but it seems to load the old code). Some caching issue? If the process is restarted, I thought the import would get the fresh code? I really want to avoid using reload(). Restarting Apache always unsticks it and picks up the changes.

#!/bin/bash

while true; do
    inotifywait . -e modify,create --exclude server.py -q
    if (($? == 0)); then
        touch server.py
    else
        exit 0
    fi
done

Am I right in thinking that this (or something like it) should work or am I barking up the wrong tree?

The root WSGI file (server.py) is quite small:

print "Server restart"

import sys, types, os, web
import api
import user # /api/user
import list # /api/list

@api.path('/info')
class info(api.Handler):
    @api.params({
        'params': {'echo': unicode }
    })
    def Post(self, data):
        return api.JSON({'info': 'foo', 'echo': data['echo']})

@api.path('/(.*)')
class notfound(api.Handler):
    def Get(self):
        api.error('404 page not found')

app = web.application(api.urls(), globals())

if __name__ == '__main__':
    app.run()
else:
    application = app.wsgifunc()