问题:代码更改后自动重新加载python Flask应用

我正在研究如何使用Python开发像样的Web应用程序。由于我不希望遇到一些高级结构,因此我选择了轻量级的Flask框架。时间会证明这是否是正确的选择。

因此,现在我已经使用mod_wsgi设置了Apache服务器,并且我的测试站点运行正常。但是,我想通过使我对py或模板文件进行的任何更改自动重新加载网站,从而加快开发流程。我看到站点的.wsgi文件中的任何更改都会导致重新加载(即使在apache配置文件中没有WSGIScriptReloading On的情况下),但我仍然必须手动进行操作(即,插入额外的换行符,保存)。编辑某些应用的py文件时,有什么方法可以引起重新加载?还是希望我使用IDE来刷新.wsgi文件?

I’m investigating how to develop a decent web app with Python. Since I don’t want some high-order structures to get in my way, my choice fell on the lightweight Flask framework. Time will tell if this was the right choice.

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?


回答 0

当前推荐的方法是使用flask命令行实用程序。

https://flask.palletsprojects.com/zh-CN/1.1.x/quickstart/#debug-mode

例:

$ export FLASK_APP=main.py
$ export FLASK_ENV=development
$ flask run

或在一个命令中:

$ FLASK_APP=main.py FLASK_ENV=development flask run

如果您想要的端口与默认(5000)添加--port选项不同。

例:

$ FLASK_APP=main.py FLASK_ENV=development flask run --port 8080

更多选项可用于:

$ flask run --help

The current recommended way is with the flask command line utility.

https://flask.palletsprojects.com/en/1.1.x/quickstart/#debug-mode

Example:

$ export FLASK_APP=main.py
$ export FLASK_ENV=development
$ flask run

or in one command:

$ FLASK_APP=main.py FLASK_ENV=development flask run

If you want different port than the default (5000) add --port option.

Example:

$ FLASK_APP=main.py FLASK_ENV=development flask run --port 8080

More options are available with:

$ flask run --help

回答 1

如果您在谈论测试/开发环境,则只需使用debug选项。发生代码更改时,它将自动重新加载flask应用程序。

app.run(debug=True)

或者,从外壳:

$ export FLASK_DEBUG=1
$ flask run

http://flask.pocoo.org/docs/quickstart/#debug-mode

If you are talking about test/dev environments, then just use the debug option. It will auto-reload the flask app when a code change happens.

app.run(debug=True)

Or, from the shell:

$ export FLASK_DEBUG=1
$ flask run

http://flask.pocoo.org/docs/quickstart/#debug-mode


回答 2

在测试/开发环境中

werkzeug调试器已经具有“自动重新加载”功能,可以通过执行以下任一操作来启用该功能:

app.run(debug=True)

要么

app.debug = True

如果需要,还可以使用单独的配置文件来管理所有设置。例如,我将’settings.py’与’DEBUG = True’选项一起使用。导入该文件也很容易。

app.config.from_object('application.settings')

但是,这不适用于生产环境。

生产环境

我个人选择Nginx + uWSGI而不是Apache + mod_wsgi是出于一些性能原因以及配置选项。该触摸重装选项允许你指定一个文件/文件夹会导致uWSGI应用程序重新加载新部署的烧瓶应用。

例如,您的更新脚本会下拉您的最新更改并触摸’reload_me.txt’文件。您的uWSGI ini脚本(由Supervisord保留-显然)在某处包含以下行:

touch-reload = '/opt/virtual_environments/application/reload_me.txt'

我希望这有帮助!

In test/development environments

The werkzeug debugger already has an ‘auto reload’ function available that can be enabled by doing one of the following:

app.run(debug=True)

or

app.debug = True

You can also use a separate configuration file to manage all your setup if you need be. For example I use ‘settings.py’ with a ‘DEBUG = True’ option. Importing this file is easy too;

app.config.from_object('application.settings')

However this is not suitable for a production environment.

Production environment

Personally I chose Nginx + uWSGI over Apache + mod_wsgi for a few performance reasons but also the configuration options. The touch-reload option allows you to specify a file/folder that will cause the uWSGI application to reload your newly deployed flask app.

For example, your update script pulls your newest changes down and touches ‘reload_me.txt’ file. Your uWSGI ini script (which is kept up by Supervisord – obviously) has this line in it somewhere:

touch-reload = '/opt/virtual_environments/application/reload_me.txt'

I hope this helps!


回答 3

如果您正在使用uwsgi运行,请查看python自动重载选项:

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设置尽可能地模仿生产。仅在Nginx后面的内置Web服务器中运行flask应用程序时,就会导致严重的网关错误。

If you’re running using uwsgi look at the python auto reload option:

uwsgi --py-autoreload 1

Example 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

Then run:

uwsgi --ini uwsgi-dev-example.ini

Note: This example also enables the debugger.

I went this route to mimic production as close as possible with my nginx setup. Simply running the flask app with it’s built in web server behind nginx it would result in a bad gateway error.


回答 4

Flask 1.0及更高版本的一些更新

热重装的基本方法是:

$ export FLASK_APP=my_application
$ export FLASK_ENV=development
$ flask run
  • 您应该使用FLASK_ENV=development(不是FLASK_DEBUG=1
  • 作为安全检查,您可以运行 flask run --debugger以确保已将其打开
  • Flask CLI现在会自动读取以下内容FLASK_APPFLASK_ENV如果.env项目根目录中文件并且安装了python-dotenv

A few updates for Flask 1.0 and above

the basic approach to hot re-loading is:

$ export FLASK_APP=my_application
$ export FLASK_ENV=development
$ flask run
  • you should use FLASK_ENV=development (not FLASK_DEBUG=1)
  • as a safety check, you can run flask run --debugger just to make sure it’s turned on
  • the Flask CLI will now automatically read things like FLASK_APP and FLASK_ENV if you have an .env file in the project root and have python-dotenv installed

回答 5

我有一个不同的主意:

第一:

pip install python-dotenv

安装python-dotenv模块,该模块将读取您的项目环境的本地首选项。

第二:

.flaskenv在您的项目目录中添加文件。添加以下代码:

FLASK_ENV=development

完成!

使用Flask项目的此配置,当您运行时flask run,您将在终端中看到以下输出:

在此处输入图片说明

当您编辑文件时,只需保存更改即可。您会看到自动重新加载在那里:

在此处输入图片说明

详细说明:

当然,您可以export FLASK_ENV=development在需要时手动点击。但是使用不同的配置文件来处理实际的工作环境似乎是一个更好的解决方案,因此我强烈建议我使用此方法。

I got a different idea:

First:

pip install python-dotenv

Install the python-dotenv module, which will read local preference for your project environment.

Second:

Add .flaskenv file in your project directory. Add following code:

FLASK_ENV=development

It’s done!

With this config for your Flask project, when you run flask run and you will see this output in your terminal:

enter image description here

And when you edit your file, just save the change. You will see auto-reload is there for you:

enter image description here

With more explanation:

Of course you can manually hit export FLASK_ENV=development every time you need. But using different configuration file to handle the actual working environment seems like a better solution, so I strongly recommend this method I use.


回答 6

Flask应用程序可以选择在调试模式下执行。在这种模式下,默认情况下启用了开发服务器的两个非常方便的模块,分别称为重载调试器。启用重新加载器后,Flask会监视项目的所有源代码文件,并在修改任何文件时自动重新启动服务器。

默认情况下,调试模式是禁用的。要启用它,FLASK_DEBUG=1在调用flask之前设置一个环境变量run

(venv) $ export FLASK_APP=hello.py for Windows use > set FLASK_APP=hello.py

(venv) $ export FLASK_DEBUG=1 for Windows use > set FLASK_DEBUG=1

(venv) $ flask run

* Serving Flask app "hello"
* Forcing debug mode on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 273-181-528

在开发过程中,启用启用重新加载程序的服务器非常有用,因为每次修改和保存源文件时,服务器都会自动重新启动并获取更改。

Flask applications can optionally be executed in debug mode. In this mode, two very convenient modules of the development server called the reloader and the debugger are enabled by default. When the reloader is enabled, Flask watches all the source code files of your project and automatically restarts the server when any of the files are modified.

By default, debug mode is disabled. To enable it, set a FLASK_DEBUG=1 environment variable before invoking flask run:

(venv) $ export FLASK_APP=hello.py for Windows use > set FLASK_APP=hello.py

(venv) $ export FLASK_DEBUG=1 for Windows use > set FLASK_DEBUG=1

(venv) $ flask run

* Serving Flask app "hello"
* Forcing debug mode on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 273-181-528

Having a server running with the reloader enabled is extremely useful during development, because every time you modify and save a source file, the server automatically restarts and picks up the change.


回答 7

要在PyCharm中实现这一目标,请将“环境变量”部分设置为:

PYTHONUNBUFFERED=1;
FLASK_DEBUG=1

对于Flask的“运行/调试配置”。

To achieve this in PyCharm set ‘Environment Variables’ section to:

PYTHONUNBUFFERED=1;
FLASK_DEBUG=1

For Flask ‘run / debug configurations’.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。