如何诊断 Python Flask 应用程序中仅当应用程序 运行 使用 uWSGI 时才会出现的问题?
How do I diagnose problems in a Python Flask application that only occur when app is run with uWSGI?
我已经编写了一个 Web 应用程序来使用 运行 uWSGI 服务器上的 Python 应用程序来控制 LED 灯带。前端和后端组件似乎可以正常工作,但是当我 运行 使用 uWSGI 而不是使用 Flask dev 网络服务器时程序会出现问题,我不确定如何诊断它们。我的代码中 wsgi 似乎有问题的两个部分是 python 日志模块(因此我很难诊断问题),以及 LED 本身的操作。我在下面列出了我的配置文件,很乐意提供任何其他可能有用的信息。
尽管开发中涉及的所有文件都已包含在 this GitHub repository 中,但代码并未处于可以轻松安装到另一个系统上的状态。这包括 Python 后端代码、HTML + CSS 和其余配置文件。预期的平台是 Raspbian Linux 运行ning on a Raspberry Pi model 3A+.
/etc/lights/lights.ini (uWSGI)
[uwsgi]
module = lights:app
chdir = /var/lights/
logto = /var/log/lights/lights.log
master = true
processes = 5
enable-threads = true
threads = 10
socket = lights.sock
chmod-socket = 666
vacuum = true
die-on-term = true
plugin = /usr/lib/uwsgi/plugins/python3_plugin.so
/etc/nginx/sites-enabled/lights (nginx)
server {
listen 80;
server_name 192.168.1.79;
access_log /var/log/nginx/lights_access.log;
error_log /var/log/nginx/lights_error.log;
root /var/lights/;
location / {
include uwsgi_params;
uwsgi_pass unix:///var/lights/lights.sock;
}
}
def main():
logging.basicConfig(level=logging.INFO, format=logFormat)
application = app
app.run()
if __name__ == "__main__":
main()
日志记录问题似乎就在这里。仅当程序作为主要 Python 脚本执行时才会设置日志记录,但对于 Python 导入则不会。 UWSGI使用Python C API调用这个函数,所以main
方法永远不会被执行。
这也解释了为什么它与 Flask 开发服务器一起工作,因为您作为 Python 脚本执行而不是导入模块。
要修复它,您只需将日志记录设置移动到主模块级别就可以了。
我已经编写了一个 Web 应用程序来使用 运行 uWSGI 服务器上的 Python 应用程序来控制 LED 灯带。前端和后端组件似乎可以正常工作,但是当我 运行 使用 uWSGI 而不是使用 Flask dev 网络服务器时程序会出现问题,我不确定如何诊断它们。我的代码中 wsgi 似乎有问题的两个部分是 python 日志模块(因此我很难诊断问题),以及 LED 本身的操作。我在下面列出了我的配置文件,很乐意提供任何其他可能有用的信息。
尽管开发中涉及的所有文件都已包含在 this GitHub repository 中,但代码并未处于可以轻松安装到另一个系统上的状态。这包括 Python 后端代码、HTML + CSS 和其余配置文件。预期的平台是 Raspbian Linux 运行ning on a Raspberry Pi model 3A+.
/etc/lights/lights.ini (uWSGI)
[uwsgi]
module = lights:app
chdir = /var/lights/
logto = /var/log/lights/lights.log
master = true
processes = 5
enable-threads = true
threads = 10
socket = lights.sock
chmod-socket = 666
vacuum = true
die-on-term = true
plugin = /usr/lib/uwsgi/plugins/python3_plugin.so
/etc/nginx/sites-enabled/lights (nginx)
server {
listen 80;
server_name 192.168.1.79;
access_log /var/log/nginx/lights_access.log;
error_log /var/log/nginx/lights_error.log;
root /var/lights/;
location / {
include uwsgi_params;
uwsgi_pass unix:///var/lights/lights.sock;
}
}
def main():
logging.basicConfig(level=logging.INFO, format=logFormat)
application = app
app.run()
if __name__ == "__main__":
main()
日志记录问题似乎就在这里。仅当程序作为主要 Python 脚本执行时才会设置日志记录,但对于 Python 导入则不会。 UWSGI使用Python C API调用这个函数,所以main
方法永远不会被执行。
这也解释了为什么它与 Flask 开发服务器一起工作,因为您作为 Python 脚本执行而不是导入模块。
要修复它,您只需将日志记录设置移动到主模块级别就可以了。