[FIXED] Python Flask application stuck with no response

Issue

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.

Solution

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

Answer Checked By – Gilberto Lyons (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published