我正在研究如何用Python开发一个不错的web应用程序。因为我不想让一些高阶结构妨碍我,所以我选择了轻量级的Flask框架。时间会证明这是否是正确的选择。
So, now I've set up an Apache server with mod_wsgi, and my test site is running fine. However, I'd like to speed up the development routine by making the site automatically reload upon any changes in py or template files I make. I see that any changes in site's .wsgi file causes reloading (even without WSGIScriptReloading On in the apache config file), but I still have to prod it manually (ie, insert extra linebreak, save). Is there some way how to cause reload when I edit some of the app's py files? Or, I am expected to use IDE that refreshes the .wsgi file for me?
在测试/开发环境中
werkzeug调试器已经有一个“自动重载”功能,可以通过执行以下操作之一来启用:
app.run(debug=True)
or
app.debug = True
如果需要,还可以使用单独的配置文件来管理所有设置。例如,我使用'settings.py'带有'DEBUG = True'选项。导入这个文件也很容易;
app.config.from_object('application.settings')
但是,这不适用于生产环境。
生产环境
就我个人而言,我选择Nginx + uWSGI而不是Apache + mod_wsgi是因为一些性能原因,也是因为配置选项。touch-reload选项允许您指定一个文件/文件夹,该文件/文件夹将导致uWSGI应用程序重新加载新部署的flask应用程序。
例如,您的更新脚本下拉最新的更改并触摸'reload_me.txt'文件。你的uWSGI ini脚本(由supervisor ord维护-显然)有这一行在它的某个地方:
touch-reload = '/opt/virtual_environments/application/reload_me.txt'
我希望这能有所帮助!
I believe a better solution is to set the app configuration. For me, I built the tool and then pushed it to a development server where I had to set up a WSGI pipeline to manage the flask web app. I had some data being updated to a template and I wanted it to refresh every X minutes (WSGI deployment for the Flask site through APACHE2 on UBUNTU 18). In your app.py or whatever your main app is, add app.config.update dictionary below and mark TEMPLATES_AUTO_RELOAD=True, you will find that any templates that are automatically updated on the server will be reflected in the browser. There is some great documentation on the Flask site for configuration handling found here.
app = Flask(__name__)
app.config.update(
TEMPLATES_AUTO_RELOAD=True
)
如果你正在使用uwsgi运行,看看python的auto reload选项:
uwsgi --py-autoreload 1
示例uwsgi-dev-example.ini:
[uwsgi]
socket = 127.0.0.1:5000
master = true
virtualenv = /Users/xxxx/.virtualenvs/sites_env
chdir = /Users/xxx/site_root
module = site_module:register_debug_server()
callable = app
uid = myuser
chmod-socket = 660
log-date = true
workers = 1
py-autoreload = 1
site_root / __init__ . py
def register_debug_server():
from werkzeug.debug import DebuggedApplication
app = Flask(__name__)
app.debug = True
app = DebuggedApplication(app, evalex=True)
return app
然后运行:
uwsgi --ini uwsgi-dev-example.ini
注意:此示例还启用调试器。
我用nginx设置尽可能地模拟生产过程。简单地运行flask应用程序,在nginx后面的web服务器中,它会导致一个糟糕的网关错误。
在测试/开发环境中
werkzeug调试器已经有一个“自动重载”功能,可以通过执行以下操作之一来启用:
app.run(debug=True)
or
app.debug = True
如果需要,还可以使用单独的配置文件来管理所有设置。例如,我使用'settings.py'带有'DEBUG = True'选项。导入这个文件也很容易;
app.config.from_object('application.settings')
但是,这不适用于生产环境。
生产环境
就我个人而言,我选择Nginx + uWSGI而不是Apache + mod_wsgi是因为一些性能原因,也是因为配置选项。touch-reload选项允许您指定一个文件/文件夹,该文件/文件夹将导致uWSGI应用程序重新加载新部署的flask应用程序。
例如,您的更新脚本下拉最新的更改并触摸'reload_me.txt'文件。你的uWSGI ini脚本(由supervisor ord维护-显然)有这一行在它的某个地方:
touch-reload = '/opt/virtual_environments/application/reload_me.txt'
我希望这能有所帮助!