我正在寻找类似于uWSGI + django自动重载模式的Flask解决方案。
我正在运行uwsgi版本1.9.5并使用该选项
uwsgi --py-autoreload 1
运行得很好
如果你正在使用命令行参数配置uwsgi
,请添加--py-autoreload=1
:
uwsgi --py-autoreload=1
如果您正在使用一个 .ini
文件来配置 uwsgi
并使用 uwsgi --ini
,请将以下内容添加到您的 .ini
文件中:
py-autoreload = 1
对于开发环境,你可以尝试使用uwsgi参数--python-autoreload。 从源代码来看,它可能只在线程模式(--enable-threads)下工作。
python-autoreload = 1
添加到我的 uwsgi.ini
文件中即可重新加载!谢谢! - JoshFinnie1
和 true
不能 互换。只有 1
对我有效。 - kungphupy-autoreload
,但文档中没有。而且,这不是被问及的属性,应该是 python-autoreload
。实际文档中有几个选项看起来是相同的,包括这两个,尽管它们没有标记为别名。这有点令人困惑,而且无论如何,描述都不清楚。如果我确切地知道发生了什么,我会提交一个文档补丁。 - kungphuwerkzeug/serving.py
中,值得一看。但基本上,主应用程序将 WSGI 服务器作为子进程生成,每秒钟检查一次每个活动的 .py
文件的状态,以查找更改。如果发现有更改,则子进程退出,父进程再次启动它 - 实际上重新加载更改。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
这是未经测试的,但应该可以工作。
import gevent.wsgi
import werkzeug.serving
@werkzeug.serving.run_with_reloader
def runServer():
gevent.wsgi.WSGIServer(('', 5000), app).serve_forever()
(您可以使用任意WSGI服务器)
我担心Flask太基础了,无法默认捆绑此类实现。
在生产中动态重新加载代码通常是不好的事情,但如果您担心开发环境,请查看此bash shell脚本http://aplawrence.com/Unixart/watchdir.html
只需将睡眠间隔更改为适合您需求的值,并用您用于重新加载uwsgi的命令替换echo命令。我在master模式下运行uwsgi,只需发送killall uwsgi命令即可。