twooster..
5
开发模式Flask的自动重新加载功能实际上是由底层的Werkzeug库提供的.相关代码是werkzeug/serving.py- 值得一看.但基本上,主应用程序将WSGI服务器生成为一个子进程,每秒对每个活动.py文件进行一次统计,查找更改.如果它看到任何,则子进程退出,并且父进程再次启动它 - 实际上重新加载chages.
你无法在uWSGI层实现类似的技术.如果您不想使用stat循环,可以尝试使用底层OS file-watch命令.显然(根据Werkzeug的代码),pyinotify是错误的,但也许看门狗工作?尝试一些事情,看看会发生什么.
编辑:
在回应评论时,我认为这很容易重新实现.以您的链接提供的示例为基础,以及来自的代码werkzeug/serving.py:
""" NOTE: _iter_module_files() and check_for_modifications() are both
copied from Werkzeug code. Include appropriate attribution if
actually used in a project. """
import uwsgi
from uwsgidecorators import timer
import sys
import os
def _iter_module_files():
for module in sys.modules.values():
filename = getattr(module, '__file__', None)
if filename:
old = None
while not os.path.isfile(filename):
old = filename
filename = os.path.dirname(filename)
if filename == old:
break
else:
if filename[-4:] in ('.pyc', '.pyo'):
filename = filename[:-1]
yield filename
@timer(3)
def check_for_modifications():
# Function-static variable... you could make this global, or whatever
mtimes = check_for_modifications.mtimes
for filename in _iter_module_files():
try:
mtime = os.stat(filename).st_mtime
except OSError:
continue
old_time = mtimes.get(filename)
if old_time is None:
mtimes[filename] = mtime
continue
elif mtime > old_time:
uwsgi.reload()
return
check_for_modifications.mtimes = {} # init static
这是未经测试的,但应该有效.