Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

112
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).

7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos