Papouche Guinslyzinho
Papouche Guinslyzinho

Reputation: 5448

How can I debug a Flask App using gunicorn

Hi when I visit my website I get This site can’t be reached

 gunicorn -w 3 flaskodesiapp:create_app 
[2020-08-09 02:35:45 -0400] [35946] [INFO] Starting gunicorn 20.0.4
[2020-08-09 02:35:45 -0400] [35946] [INFO] Listening at: http://127.0.0.1:8000 (35946)
[2020-08-09 02:35:45 -0400] [35946] [INFO] Using worker: sync
[2020-08-09 02:35:45 -0400] [35948] [INFO] Booting worker with pid: 35948
[2020-08-09 02:35:45 -0400] [35949] [INFO] Booting worker with pid: 35949
[2020-08-09 02:35:45 -0400] [35950] [INFO] Booting worker with pid: 35950

but executing python3 run.py runs the code with no issue. Therefore, I am able to see the app. run.py

from flaskodesiapp import create_app

app = create_app()

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5090, debug=True)

wsig.py

from flaskodesiapp import create_app
from .config import Config

if __name__ == "__main__":
    app = create_app(config=Config)
    app.run(host="0.0.0.0", port=5090, debug=True)

create_app comes from init.py

from flask import Flask
from flaskodesiapp.config import Config

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)

    from flaskodesiapp.odesi.routes import odesi
    from flaskodesiapp.errors.handlers import errors

    app.register_blueprint(odesi)
    app.register_blueprint(errors)

    return app

Config contains only smtp credentials.

Upvotes: 0

Views: 2446

Answers (1)

Arshad Alam
Arshad Alam

Reputation: 69

Try this below code

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config['DEBUG'] = True
    ...

Even if you set app.debug = True, you will still only get an empty page with the message Internal Server Error if you run with gunicorn testserver:app. The best you can do with gunicorn is to run it with gunicorn --debug testserver:app. That gives you the trace in addition to the Internal Server Error message. However, this is just the same text trace that you see in the terminal and not the Flask debugger.

Adding the if name ... section to the testserver.py and running python testserver.py to start the server in development gets you the Flask debugger.

So, will suggest you do not use gunicorn in development environment, it will not help you in the proper way.

Gunicorn is a pre-forking environment and apparently the Flask debugger doesn't work in a forking environment.

Upvotes: 1

Related Questions