Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask debug=True does not work when going through uWSGI

Tags:

python

flask

wsgi

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() 
like image 370
Flaviu Avatar asked Apr 28 '12 15:04

Flaviu


People also ask

How do I set debug mode on true flask?

To enable the debugger, run the development server with the FLASK_ENV environment variable set to development . This puts Flask in debug mode, which changes how it handles some errors, and enables the debugger and reloader. FLASK_ENV can only be set as an environment variable.

Does flask use uWSGI?

Step 3 — Setting Up a Flask Application. Now that you are in your virtual environment, you can install Flask and uWSGI and get started on designing your application. First, install wheel with the local instance of pip to ensure that your packages will install even if they are missing wheel archives: pip install wheel.


Video Answer


2 Answers

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.

like image 81
gonz Avatar answered Sep 28 '22 02:09

gonz


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.

like image 34
Edwardr Avatar answered Sep 28 '22 00:09

Edwardr