Thursday 24 August 2023

Where is the bottleneck in these 10 requests per second with Python Bottle + Javascript fetch?

I am sending 10 HTTP requests per second between Python Bottle server and a browser client with JS:

import bottle, time
app = bottle.Bottle()
@bottle.route('/')
def index():
    return """<script>
var i = 0;
setInterval(() => {
    i += 1;
    let i2 = i;
    console.log("sending request", i2);
    fetch("/data")
        .then((r) => r.text())
        .then((arr) => {
            console.log("finished processing", i2);
        });
}, 100);
</script>"""
@bottle.route('/data')
def data():
    return "abcd"
bottle.run(port=80)

The result is rather poor:

sending request 1
sending request 2
sending request 3
sending request 4
finished processing 1
sending request 5
sending request 6
sending request 7
finished processing 2
sending request 8
sending request 9
sending request 10
finished processing 3
sending request 11
sending request 12

Why does it fail to process 10 requests per second successfully (on an average i5 computer): is there a known bottleneck in my code?

Where are the 100 ms lost per request, that prevent the program to keep a normal pace like:

sending request 1
finished processing 1
sending request 2
finished processing 2
sending request 3
finished processing 3

?

Notes:

  • Tested with Flask instead of Bottle and the problem is similar

  • Is there a simple way to get this working:

    • without having to either monkey patch the Python stdlib (with from gevent import monkey; monkey.patch_all()),

    • and without using a much more complex setup with Gunicorn or similar (not easy at all on Windows)?

    ?



from Where is the bottleneck in these 10 requests per second with Python Bottle + Javascript fetch?

No comments:

Post a Comment