JordonWilks
JordonWilks

Reputation: 61

How is Flask able to handle concurrent requests in this experiment?

I am trying to wrap my head around how Flask works. In particular, I am trying to understand how a single Flask app handles concurrent requests. It seems like according to Can I serve multiple clients using just Flask app.run() as standalone? and How many concurrent requests does a single Flask process receive? a single Flask app is able to only handle one request at a time. However, a quick experiment I ran doesn't seem to match that mental model.

Here is a Flask app with a single endpoint that does a blocking call:

@app.route('/pause')
def pause():
  time.sleep(5)
  return jsonify()

app.run()

For my experiment, I opened 2 terminals and hit the endpoint using curl in parallel. I would expect that the first request would finish in 5 sec, then the second request would start, and then finish after 5 more sec (so the total time for both requests is 10 sec). However, what actually happens is that the second requests finishes immediately after the first request. Doesn't this mean that both requests ran in parallel? If so, is Flask running these in separate threads or processes since Python has a GIL?

Upvotes: 6

Views: 2753

Answers (1)

technical_geek
technical_geek

Reputation: 11

According to my knowledge, by default, Fask runs in multithreaded mode, so each request lands on a new thread.

Upvotes: 0

Related Questions