Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

306
Vistas
Timeout option is not working in nodejs spawn child process

I was trying to run the Python program through spawn child process function and trying to give timeout option for TLE cases. But Timeout option is not working. Code is running fine for smaller inputs, but when running on input size > 1e9 it should terminate in 5 seconds according to my code. But when sending request it gets hanging around infinitely.

My Nodejs code

        let python = spawn('python3', [`./uploads/${codefile}`], { timeout: 5000 });

        python.stdout.on('data', (data) => {
            res.status(200).json({ Success: data.toString() });
            deleteFiles(req.files)
            return;
        });

        python.stderr.on('data', (data) => {
            res.status(200).json({ Error: data.toString() });
            deleteFiles(req.files)
            return;
        });

        python.on('error', (err) => {
            // will handle Sigterm signal here
            //  res.status(200).json({
            //      errorMessage: "Time Limit Exceeded, Please Optimised Your Code",
            //      errorType: "Time Limit Exceeded"
            //  });
            console.log(err)
            res.status(500).send("Internal Server Error");
            deleteFiles(req.files);
            return;
        });

My Python code

n=10000000000
b=0;
for i in range(n):
    b+=1
print("calculated sum is",b)

I have also tried exec function with timeout option and it is working fine there but due to security reasons I do not wan't to use it. I am using Node.js version 16.15.1

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Uh, if the timeout in spawn isn't working.. you can use setTimeout logic to kill the program that would have its Time Limit Exceeded

        let python = spawn('python3', [`./uploads/${codefile}`]);
        let timeout=setTimeout(()=>{
            python.kill() //kills the program that is taking too long
            deleteFiles(req.files)
            res.status(200).json({
                errorMessage: "Time Limit Exceeded, Please Optimised Your Code",
                errorType: "Time Limit Exceeded"
            });
        },5000)
        
        //I'd add a "python.on('end',some_callback)" but you seem to return the response on one chunk of data
        python.stdout.on('data', (data) => {
            res.status(200).json({ Success: data.toString() });
            deleteFiles(req.files)
            clearTimeout(timeout) //preventing the timeout from calling since it didn't take too long
            return;
        });

        python.stderr.on('data', (data) => {
            res.status(200).json({ Error: data.toString() });
            deleteFiles(req.files)
            clearTimeout(timeout) //preventing the timeout from calling since it didn't take too long
            return;
        });

        python.on('error', (err) => {
            // will handle Sigterm signal here
            //  res.status(200).json({
            //      errorMessage: "Time Limit Exceeded, Please Optimised Your Code",
            //      errorType: "Time Limit Exceeded"
            //  });
            console.log(err)
            clearTimeout(timeout) //preventing the timeout from calling since it didn't take too long
            res.status(500).send("Internal Server Error");
            deleteFiles(req.files);
            return;
        });
about 4 years ago · Juan Pablo Isaza Denunciar
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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda