4

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.

2 Answers 2

4

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.
Sign up to request clarification or add additional context in comments.

Comments

4

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.

2 Comments

Thanks. Do you know the way to setup and always running Flask App using gunicorn (even after server restart)?
There are Gunicorn Settings that you could look into, but I've only used few of the flags here. In general, the Gunicorn server is persistent and settings can be tweaked with a config file. My recommendation is to schedule a cron job or a shell script that starts the Gunicorn server(Flask app) after the Linux server restarts or boots.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.