• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

218
Vistas
XHR HTTP request performance between Python Flask and JavaScript

During benchmarking tests, I noticed that sequential XHR HTTP requests between JavaScript and Python Flask are quite slow on Windows, with a factor 10 less of what I expected: only ~ 80 MB/sec between a Python Flask server and Chrome on the same computer (localhost). I have a rather standard i5 CPU.

Is there some obvious bottleneck or an obvious misuse of the API in the following code that make the XHR HTTP requests limited at less than 100 MB/sec on an average computer?

import time, flask, numpy as np
app = flask.Flask("Test")
i, b, t0 = 0, 0, time.time()
x = np.random.rand(1000,1000).tobytes()  # fixed bytes to avoid measuring the random generation time
                                         # 7.6 MB
@app.route('/data')
def data():
    global i, b
    i += 1
    b += len(x)
    if i % 10 == 0:
        print("%.1f MB, %.1f MB/sec" % (b / 2**20, b / 2**20 / (time.time()-t0)))
    return x
@app.route('/')
def index():
    return """
<html><head></head><body><script>
function getdata() {
    req = new XMLHttpRequest();
    req.open("GET", "/data", true);
    req.responseType = "arraybuffer";
    req.onload = function (oEvent) {
        var arrayBuffer = req.response;
        if (arrayBuffer) {
            // var byteArray = new Uint8Array(arrayBuffer);
            getdata();       // sequential: new request only when the previous data has arrived
        }
    };
    req.send(null);
}
getdata();
</script></body></html>
"""
app.run()

(Run the script, go on http://127.0.0.1:5000/ and check the Python log).

about 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda