Debugging a Flask application running in Gunicorn

I am working on a new development platform using nginx / gunicorn and Flask for my application.

In different ways, everything works just fine - the problem I ran into is debugging the Flask layer. When an error appears in my code, I just get a direct 500 error returned to the browser and nothing is displayed on the console or in my logs.

I tried many different configurations / options. I suggest that I should skip something obvious.

My gunicorn.conf:

import os bind = '127.0.0.1:8002' workers = 3 backlog = 2048 worker_class = "sync" debug = True proc_name = 'gunicorn.proc' pidfile = '/tmp/gunicorn.pid' logfile = '/var/log/gunicorn/debug.log' loglevel = 'debug' 

An example of some flash drive code that borks-testserver.py:

 from flask import Flask from flask import render_template_string from werkzeug.contrib.fixers import ProxyFix app = Flask(__name__) @app.route('/') def index(): n = 1/0 return "DIV/0 worked!" 

And finally, the command to run the flash application in gunicorn:

 gunicorn -c gunicorn.conf.py testserver:app 

Thanks y'all

+47
python flask gunicorn
Jan 21 2018-12-12T00:
source share
5 answers

Flask configuration is completely separate from the gun. Following the Flask documentation in the configuration files , a good solution would be to change my source:

 app = Flask(__name__) app.config.from_pyfile('config.py') 

And in config.py:

 DEBUG = True 
+40
Jan 22 '12 at 5:03
source share

The decision to make does not work for me.

Gunicorn is a preliminary infrastructure and, apparently, the Flask debugging program does not work in a forking environment .

Attention

Despite the fact that the interactive debugger does not work in (which makes it almost impossible to use production servers) [...]

Even if you set app.debug = True , you still get only a blank page with the message Internal Server Error if you use gunicorn testserver:app . The best you can do with a gun is to run it with gunicorn --debug testserver:app . This gives you a trace in addition to reporting an internal server error. However, these are only the same text traces that you see in the terminal, not the Flask debugger.

Adding an if __name__ ... section to testerver.py and running python testserver.py to start the server in development will give you a Flask debugger. In other words, do not use gunicorn in development if you want a Flask debugger.

 app = Flask(__name__) app.config['DEBUG'] = True if __name__ == '__main__': app.run() 


Tip for Heroku users:

Personally, I still like to use foreman start instead of python testserver.py , since it sets all env variables for me . To get this done:

Procfile Content

 web: bin/web 

The contents of bin/web , the file refers to the root of the project

 #!/bin/sh if [ "$FLASK_ENV" == "development" ]; then python app.py else gunicorn app:app -w 3 fi 

In development, create an .env file relative to the project root with the following contents (docs here )

 FLASK_ENV=development DEBUG=True 

Also, be sure to change the line app.config['DEBUG']... in testserver.py so that it will not run the flag in debug mode during production.

 app.config['DEBUG'] = os.environ.get('DEBUG', False) 
+64
Dec 19 '12 at 6:26
source share

There is a simpler solution for Heroku users than creating a bin / web script, as Nick suggested.

Instead of foreman start just use foreman run python app.py if you want to debug your application during development.

+23
Aug 23 '13 at 23:15
source share

Try setting the debug flag in the run command like this:

 gunicorn -c gunicorn.conf.py --debug testserver:app 

and save DEBUG = True in your Flask application. There must be a reason why your debugging option is not applied from the configuration file, but for now you should go to the note above.

+1
Jan 23 2018-12-23T00:
source share

I had a similar problem when starting a bulb under firing. I did not see the stacks in the browser (I had to look at the logs every time). Setting DEBUG, FLASK_DEBUG or anything mentioned on this page does not work. Finally I did this:

 app = Flask(__name__) app.config.from_object(settings_map[environment]) if environment == 'development': from werkzeug.debug import DebuggedApplication app_runtime = DebuggedApplication(app, evalex=False) else: app_runtime = app 

Note. evalex is disabled because interactive debbugging will not work with forking (gunicorn).

0
Dec 30 '17 at 11:56 on
source share



All Articles