简体   繁体   中英

How do I diagnose problems in a Python Flask application that only occur when app is run with uWSGI?

I have written a web application to control an LED strip using a Python application that runs on a uWSGI server. The front end and back end components seem to work correctly but there are issues with the program that occur when I run it with uWSGI and not when using the Flask dev webserver and I am unsure how to go about diagnosing them. The two parts of my code that wsgi seems to have a problem with are the python logging module (hence my difficulty diagnosing the problem), and the operation of the LED's themselves. I have listed my configuration files below and am happy to provide any other information that might be helpful.

The code isn't in a state where it can be installed easily on another system easily although all of the files involved in the development have been included in this GitHub repository . This includes the Python back end code, the HTML + CSS, and the remaining configuration files. The intended platform is Raspbian Linux running 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()

The logging issue seems to be here. Logging is only set up if the program is executed as the main Python script, but not for Python imports. UWSGI calls this function using the Python C API, so the main method will never be executed.

This also explains why it works with the Flask development server, as you execute as a Python script instead of importing the module.

To fix it, you can just move logging setup to the main module level and you should be good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM