1

The following code takes only 2 seconds when 10 requests are requested at the same time (ab -n 10 -c 10 http://127.0.0.1:5000/). Shouldn't it be 10 seconds because of GIL?

from flask import Flask
from time import sleep

app = Flask(__name__)

@app.route("/")
def hello_world():
    sleep(1)
    return "<p>Hello, World!</p>"

if __name__ == "__main__":
    app.run()

# ab -n 10 -c 10 http://127.0.0.1:5000/
# This is ApacheBench, Version 2.3

# Server Software:        Werkzeug/2.0.1
# Server Hostname:        127.0.0.1
# Server Port:            5000

# Concurrency Level:      10
# Time taken for tests:   2.021 seconds
# Complete requests:      10

Is sleep() blocking? And the code doesn't use asyncio even if it yields the control when calling sleep()?

ca9163d9
  • 27,283
  • 64
  • 210
  • 413
  • 1
    I don't know a lot about the GIL, but I would think that while one (or several) threads are sleeping, that is the perfect situation for other threads to get work done. – John Gordon Nov 11 '21 at 02:59
  • Is `sleep()` blocking? And the code doesn't use asyncio if it yield the control when calling `sleep()`? – ca9163d9 Nov 11 '21 at 03:01

0 Answers0