Forums

OSError: write error

I keep on seeing OSError: write error in my error log... what does it mean?

The 'OSError: write error' message is logged when a client disconnects from the server before the server has finished sending the response. This can happen because the site is slow and a user stops the page from loading or because the timeout on a client is too small.

Slowness can be caused by 2 things:

  1. You do not have enough workers to handle the traffic that is going to your site at particular times. When all of your workers are busy, any new requests are placed in a queue and have to wait for a worker to be free.

  2. Your code is doing something that is inherently slow.

You can investigate this by finding the views that are slow. Your access logs have a response-time for every access - it includes the time that a request spent in the queue waiting for a worker. If you add timing logging to the views that appear to be slow (prints to stderr will appear in your error log), you will be able to see where the views are spending their time and tell how long each request spent in the queue. Then you can either add workers or use the timing information to optimize your code so that it runs more quickly

Oh I see, let me investigate further to see if I need more workers or its the code that need to be optimized. Thank you.

How can I solve that this code slows down the server's response since it keeps loading and sends me an error and only shows the OSError: write error?

@app.route("/predict", methods=['GET', 'POST'])

@login_required

def predict():

if request.method == 'POST':

    file = request.files['image']

    correo = request.form['correoP']

    imagen_original = Image.open(file)

    imagen_redimensionada = imagen_original.resize((512, 512))

    print("@@ Input posted = ", file)


    with tempfile.TemporaryDirectory() as temp_dir:
        filename = tempfile.NamedTemporaryFile(suffix=".jpg", dir=temp_dir).name
        file_path = os.path.join(temp_dir, filename)
        imagen_redimensionada.save(file_path)
        pred, porcen, id_plaga, foto_base = prediccion_tomate(file)
        tomate = Tomate(0, None, file_path, correo)
        insertado = Modeltomate.insertarTomate(db, tomate)
        datos = json.loads(insertado)
        if insertado:
            flash("Tomate insertada correctamente...")
            control = Control(0, None, None, None, None, id_plaga, None, None)
            plaga = Plaga(id_plaga, None, None)
            tomate = Tomate(datos['datos'], None, None,None)
            lista_controles = Modelcontrol.listarcontrolsegunplaga(db, control)
            datos_plaga = Modelplaga.listarPlaga(db, plaga)
            tomate_ingresado = Modeltomate.listarTomate(db, tomate)
            img_tomate = base64.b64encode(tomate_ingresado[0][2]).decode('utf-8')
            if lista_controles:
                fotos = [resultado[6] for resultado in lista_controles]
                fotos_base64 = [base64.b64encode(foto).decode('utf-8') for foto in fotos]
                lista_controles_con_imagenes = [(control, foto_base64) for control, foto_base64 in
                                                zip(lista_controles, fotos_base64)]
                return render_template("seguimiento.html", foto_base=foto_base, pred_output=pred,
                                       porc=porcen, foto=filename,
                                       trat=lista_controles_con_imagenes, id_hoja=datos['datos'],
                                       fotos=fotos_base64, datos_plaga=datos_plaga,img_tomate=img_tomate,ti=tomate_ingresado)
            else:
                return render_template("seguimiento.html", foto_base=foto_base, pred_output=pred,
                                       porc=porcen, foto=filename, id_hoja=datos['datos'],img_tomate=img_tomate,ti=tomate_ingresado)
        else:
            flash("No se ha podido insertar el tomate")
            return render_template("indexSesion.html")
else:
    flash("No se ha enviado ningĂșn dato...")
    return render_template("indexSesion.html")

Take a look at our help page