1

On many sites, including Stack Overflow, people say that Flask's built-in server (started by app.run()) only serves requests serially. Many say that if a handler takes a few seconds to respond, then the server would not be able to serve other requests in the meantime. Why am I seeing a totally opposite behavior with Flask 1.0.3?

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


@app.route('/hello')
def hello():
    return 'hello'


app.run()

While the slow handler is sleeping, I can successfully fire off requests and receive responses for hello. Why is that? Can the development server handle multiple requests at a time then? I so, what part of Flask enables that?

genexpr
  • 91
  • 2
  • 8
  • What if you try triggering the `slow` handler successively? Just curious. – absolutelydevastated Jul 05 '19 at 02:41
  • They also seem to run concurrently. If I hit `slow`, then hit it from another client 2 seconds after, then the second call would return 3 seconds after the first one, assuming each call takes 5 seconds. – genexpr Jul 05 '19 at 02:55
  • sleep releases the GIL as mentioned [here](https://bugs.python.org/issue23251).) that could be a possible reason – new-dev-123 Jul 05 '19 at 02:56

1 Answers1

1

You should probably try with a blocking lock, like this:

release = datetime.now() + timedelta(seconds=5)
while datetime.now() < release:
  continue
return 'slow'

sleep can be non-blocking, this just keeps it busy, the same way a heavy calculation would.

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49