防止 Flask 应用程序的开发服务器意外重新加载

Prevent flask app's development server from unwanted reloads

我是 运行 在 http://127.0.0.1:7000/ 使用命令 py main.py 的烧瓶应用程序。在我的文件中,最后一行是 app.run(debug=True, port=7000)。我已经宣布 app = Flask(__name__)。我每次都需要重新启动服务器。但是服务器会重新加载不需要的文件,甚至不允许我查看页面!日志是:

F:\Github SPRINGREEN\web-app>py main.py
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with windowsapi reloader
 * Debugger is active!
 * Debugger PIN: 140-651-644
 * Running on http://127.0.0.1:7000/ (Press CTRL+C to quit)
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pip\_vendor\toml\encoder.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pip\_vendor\toml\ordered.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pip\_vendor\toml\tz.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pip\_vendor\toml\__init__.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pip\_vendor\urllib3\connection.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pip\_vendor\urllib3\connectionpool.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pip\_vendor\urllib3\exceptions.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pip\_vendor\urllib3\fields.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pip\_vendor\urllib3\filepost.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pip\_vendor\urllib3\poolmanager.py', reloading
 * Restarting with windowsapi reloader
 * Debugger is active!
 * Debugger PIN: 140-651-644
 * Running on http://127.0.0.1:7000/ (Press CTRL+C to quit)
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_win32comgenpy.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth__tkinter.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\hooks\rthooks\__init__.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\lib\modulegraph\find_modules.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\lib\modulegraph\util.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\lib\modulegraph\zipio.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\lib\modulegraph\_compat.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\lib\modulegraph\__init__.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\lib\modulegraph\__main__.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\utils\cliutils\archive_viewer.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\utils\cliutils\bindepend.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\utils\cliutils\grab_version.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\utils\cliutils\makespec.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\utils\cliutils\set_version.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\utils\cliutils\__init__.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\PyInstaller\utils\hooks\django.py', reloading
 * Restarting with windowsapi reloader
 * Debugger is active!
 * Debugger PIN: 140-651-644
 * Running on http://127.0.0.1:7000/ (Press CTRL+C to quit)
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\advanced\resolve_peer.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\advanced\save_file.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\advanced\send.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\advanced\__init__.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\auth\accept_terms_of_service.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\auth\check_password.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\auth\connect.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\auth\disconnect.py', reloading
 * Restarting with windowsapi reloader
 * Debugger is active!
 * Debugger PIN: 140-651-644
 * Running on http://127.0.0.1:7000/ (Press CTRL+C to quit)
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\leave_chat.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\mark_chat_unread.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\pin_chat_message.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\promote_chat_member.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\restrict_chat_member.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\set_administrator_title.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\set_chat_description.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\set_chat_permissions.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\set_chat_photo.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\set_chat_title.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\set_slow_mode.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\unarchive_chats.py', reloading
 * Detected change in 'C:\Users\new\AppData\Local\Programs\Python\Python39-32\Lib\site-packages\pyrogram\methods\chats\unban_chat_member.py', reloading

但是,我正在处理目录中的文件:F:\flask-app\app-1\main.py,文件夹 F:\flask-app\app-1 在我的 VS 代码中打开。当我对文件夹中存在的文件进行更改时,我只想重新加载烧瓶:F:\flask-app\app-1 而不是任何其他文件!有什么办法吗?部分规格:


C:\User\new>python -V
Python 3.9.1

C:\User\new>python -m flask --version
Python 3.9.1
Flask 1.1.2
Werkzeug 1.0.1

C:\User\new>cmd
Microsoft Windows [Version 10.0.19042.985]
(c) Microsoft Corporation. All rights reserved.

我正在使用 Windows 32 位。

这有点奇怪,但我认为这可能是因为您使用的是全局 python 解释器。我建议创建一个虚拟环境,例如 python -m venv env 接着 .\env\Scripts\activate

然后在那里安装所有包并再次尝试 运行 flask。