0

我有一个设置,使得 nginx 服务器将控制权传递给 uWsgi,uWsgi 使用我的 xml 配置文件中的以下内容启动 pylons 应用程序:

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

一切都运行良好,我能够使用相关 ini 文件中的以下内容将其设置为调试模式,例如:

debug = true

除了调试模式只打印出错误,并且每次触摸文件时都不会重新加载代码。如果我直接通过粘贴运行,我可以使用该--reload选项,但是通过 uWsgi 会使事情复杂化。

有谁知道告诉 uWsgi 告诉 paste 设置--reload选项的方法,或者直接在 paste .ini 文件中执行此操作?

4

2 回答 2

1

我使用类似下面的代码来解决这个问题,monitorFiles(...) 方法在应用程序初始化时被调用,它监视文件,当它看到变化时发送 TERM 信号。

我仍然更喜欢使用 paster 的 --reload 参数的解决方案,因为我认为这个解决方案有错误:

    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)
于 2011-03-15T06:44:59.107 回答
0

使用 0.9.7 树中的信号框架

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

自动重新加载的示例:

import uwsgi

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

def application(env, start_response):
    ...
于 2011-03-15T08:54:35.663 回答