Search code examples
pythonasynchronousflasksynchronous

Synchronous api tasks on Python Flask


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)?


Solution

  • 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.