Synchronous api tasks on Python Flask

Viewed 3716

I know that we cannot make async calls using Flask. Flask supports synchronous calls. Does that mean a user's request waits the other requests that started before?

For example, I have a post method like this and assume that update method takes 10 seconds to execute:

@app.route('/update', methods = ['POST'])
def update():
    # CODE

Assume that 100 users are making a post request on update method at the same time. The 100th user will wait for the previous requests (99 users * 10 sec = 990 seconds)?

2 Answers

Conceptually Yes you are correct but in the real world, it depends on your deployment of the flask application

Like we use Gunicorn which is a WSGI HTTP Server that usually lives between a reverse proxy Nginx and a web application such as Django or Flask. Gunicorn spawns a number of workers and The role of the workers is to handle HTTP requests.

First, depending on what you mean by "async", yes, you can make HTTP requests to a Flask app that arranges to do work asynchronously. That's what tasks queues like celery and Rq are for. There's a good walk-through of using Rq in chapter 22 of the Flask Mega Tutorial.

Second, if you only have a single process, single thread web server, then yes, requests are handled sequentially. In practice, though, you'll deploy behind something like uwsgi or gunicorn, which manage multiple processes running your app.

Related