Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

307
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda