开发者

Development Mode For uWSGI/Pylons (Reload new code)

开发者 https://www.devze.com 2023-02-17 07:35 出处:网络
I have a setup such that an nginx server passes control off to uWsgi, which launches a pylons app using the following in my xml configuration file:

I have a setup such that an nginx server passes control off to uWsgi, which launches a pylons app using the following in my xml configuration file:

<ini-paste>...</ini-paste>

Everything is working nicely, and I was able to set it to debug mode using the following in the associated ini file, like:

debug = true

Except debug mode only prints out errors, and doesn't reload the code everytime a file has been touched. If I was runn开发者_运维知识库ing directly through paste, I could use the --reload option, but going through uWsgi complicates things.

Does anybody know of a way to tell uWsgi to tell paste to set the --reload option, or to do this directly in the paste .ini file?


I used something like the following code to solve this, the monitorFiles(...) method is called on application initialization, and it monitors the files, sending the TERM signal when it sees a change.

I'd still much prefer a solution using paster's --reload argument, as I imagine this solution has bugs:

    import os
    import time
    import signal

    from deepthought.system import deployment
    from multiprocessing.process import Process

    def monitorFiles():
        if deployment.getDeployment().dev and not FileMonitor.isRunning:
            monitor = FileMonitor(os.getpid())
            try: monitor.start()
            except: print "Something went wrong..."

    class FileMonitor(Process):

        isRunning = False

        def __init__(self, masterPid):
            self.updates = {}
            self.rootDir = deployment.rootDir() + "/src/python"
            self.skip = len(self.rootDir)
            self.masterPid = masterPid
            FileMonitor.isRunning = True
            Process.__init__(self)

        def run(self):
            while True:
                self._loop()
                time.sleep(5)

        def _loop(self):
            for root, _, files in os.walk(self.rootDir):
                for file in files:
                    if file.endswith(".py"):
                        self._monitorFile(root, file)

        def _monitorFile(self, root, file):
            mtime = os.path.getmtime("%s/%s" % (root, file))
            moduleName = "%s/%s" % (root[self.skip+1:], file[:-3])
            moduleName = moduleName.replace("/",".")
            if not moduleName in self.updates:
                self.updates[moduleName] = mtime
            elif self.updates[moduleName] < mtime:
                print "Change detected in %s" % moduleName
                self._restartWorker()
                self.updates[moduleName] = mtime

        def _restartWorker(self):
            os.kill(self.masterPid, signal.SIGTERM)


Use the signal framework in 0.9.7 tree

http://projects.unbit.it/uwsgi/wiki/SignalFramework

An example of auto-reloading:

import uwsgi

uwsgi.register_signal(1, "", uwsgi.reload)
uwsgi.add_file_monitor(1, 'myfile.py')

def application(env, start_response):
    ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号