Flask debug=True does not work when going through uWSGI
Asked Answered
K

4

41

I call app.run(debug=True) in my flask file.

and I have it deployed with uWSGI and nginx (I followed these instructions)

uwsgi -s /tmp/uwsgi.sock -w flask_file_name:app -H /path/to/virtual/env --chmod-socket 666

But when I get an error, I don't get any debug information in the browser or in the uWSGI log.

Any ideas?

flask_file_name.py:

from flask import Flask, make_response, Response, jsonify
import json

app = Flask(__name__)
app.debug = True

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == '__main__':
    app.run()
Kissner answered 28/4, 2012 at 15:21 Comment(1)
Can you post flask_file_name.py?Albumenize
S
26

According to the Flask mailing list you cannot use Flask's debug option with uWSGI, because it's not to be used in a forking environment.

You see 502 because flask/werkzeug do not send any data to the webserver, so nginx will returns a 502.

You can emulate the debugger using --catch-exceptions option in uWSGI (but please do not do it in production)

So, the reason you're seeing 502s will be because of that. The fix would be to add --catch-exceptions to uWSGI on execution.

Style answered 5/5, 2012 at 9:12 Comment(3)
You are the man! It's still not formatted like it was without uWSGI, but I do get the error now. Thanks so much!Kissner
Yes, but also see the comment by gonz, below. :)Guarani
Worked for me if I set debug mode in py file: app.debug = True.Hyalite
K
35

This question is old, but I'll post this for future reference...

If you want to get the werkzeug error page to work with uwsgi, try using werkzeug's DebuggedApplication middleware:

from werkzeug.debug import DebuggedApplication
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)

That should do the trick but DO NOT FORGET to do this ONLY in development environments.

Khosrow answered 24/7, 2013 at 16:29 Comment(3)
This worked for me, the other answers didn't. Thanks.Coelom
To make the debugger prompt work, you have to use a single worker in uwsgi (--workers 1, --threads 4)Kelleher
It worked for me if only I I set debug mode: app.debug = TrueHyalite
S
26

According to the Flask mailing list you cannot use Flask's debug option with uWSGI, because it's not to be used in a forking environment.

You see 502 because flask/werkzeug do not send any data to the webserver, so nginx will returns a 502.

You can emulate the debugger using --catch-exceptions option in uWSGI (but please do not do it in production)

So, the reason you're seeing 502s will be because of that. The fix would be to add --catch-exceptions to uWSGI on execution.

Style answered 5/5, 2012 at 9:12 Comment(3)
You are the man! It's still not formatted like it was without uWSGI, but I do get the error now. Thanks so much!Kissner
Yes, but also see the comment by gonz, below. :)Guarani
Worked for me if I set debug mode in py file: app.debug = True.Hyalite
A
25

The problem is uwsgi does not call app.run(). It calls app(). So instead you can do this:

from flask import Flask
app = Flask(__name__)
app.debug = True
Albumenize answered 28/4, 2012 at 15:33 Comment(2)
Thanks. That helps. Now if I have a syntax error, it redirects to the nginx static 50x.html error page. How can I get flask to handle that?Kissner
Worked for me if I set catch-exceptions = true in uwsgi.ini.Hyalite
R
3

For me it only worked after I combined the two answers above like this:

from flask import Flask
app = Flask(__name__)

from werkzeug.debug import DebuggedApplication
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)

app.debug = True
Rockbound answered 19/6, 2018 at 20:44 Comment(1)
Worked for me, this is the best solution for me.Hyalite

© 2022 - 2024 — McMap. All rights reserved.