Python Flask application stuck with no response

Question:

I have a Flask application running on a linux server and noticed that occasionally it get stuck when sending POST request to it and then go to GET and try POST again (then it get stuck). It get ‘un-stuck’ if I do GET again (then last POST that was stuck gets completed).

First part of Flask app is:

@app.route('/myroute', methods=['GET','POST'])
def myfunction():
    if request.method == 'POST':
        ...
    else:
        ...

Starting it with:
FLASK_APP=myflask.py FLASK_DEBUG=1 python -m flask run --port 8300 --host=0.0.0.0 --no-reload.

Also did setup parallel threads with:

if __name__ == '__main__':
    app.run(threaded=True)

but that did not prevent for getting stuck.

Asked By: Joe

||

Answers:

The code inside if __name__ == '__main__' is not run when you start the application using python -m flask run ....

So the threaded=True-part is not in effect.

Use the --with-threads command-line switch.

$ flask run --help
Usage: flask run [OPTIONS]

  Runs a local development server for the Flask application.

  This local server is recommended for development purposes only but it can
  also be used for simple intranet deployments.  By default it will not
  support any sort of concurrency at all to simplify debugging.  This can be
  changed with the --with-threads option which will enable basic
  multithreading.

  The reloader and debugger are by default enabled if the debug flag of
  Flask is enabled and disabled otherwise.

Options:
  -h, --host TEXT                 The interface to bind to.
  -p, --port INTEGER              The port to bind to.
  --reload / --no-reload          Enable or disable the reloader.  By default
                                  the reloader is active if debug is enabled.
  --debugger / --no-debugger      Enable or disable the debugger.  By default
                                  the debugger is active if debug is enabled.
  --eager-loading / --lazy-loader
                                  Enable or disable eager loading.  By default
                                  eager loading is enabled if the reloader is
                                  disabled.
  --with-threads / --without-threads
                                  Enable or disable multithreading.
  --help                          Show this message and exit.
Answered By: codeape

The WSGI server provided with Flask is recommended only for development purposes and is not suitable for heavy load requests. Since you mentioned Linux servers, its good practice to use Gunicorn. Gunicorn is speedy and light on server resources. Once installed with pip install gunicorn, you can easily assign worker threads as your application continues to receive higher requests. The below example assigns 4 worker processes(-w 4).

gunicorn -w 4 -b 127.0.0.1:4000 myproject:app

You can find more information on similar Standalone WSGI containers on the official Flask documentation.

Answered By: amanb

use windows.
When I first started flask, I used a window server. And for months, it has been running as developmental server and never stopped responding.

Answered By: user19893172
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.