WSGI process reload modules

橙三吉。 提交于 2019-12-11 06:39:15

问题


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()

来源:https://stackoverflow.com/questions/33845985/wsgi-process-reload-modules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!