Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

299
Views
La opción de tiempo de espera no funciona en el proceso secundario de generación de nodejs

Estaba tratando de ejecutar el programa Python a través de la función de proceso secundario de generación y tratando de dar la opción de tiempo de espera para los casos de TLE. Pero la opción de tiempo de espera no funciona. El código funciona bien para entradas más pequeñas, pero cuando se ejecuta en tamaño de entrada> 1e9, debería terminar en 5 segundos según mi código. Pero al enviar la solicitud, se queda dando vueltas infinitamente.

Mi código Nodejs

 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; });

Mi código Python

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

También probé la función exec con la opción de tiempo de espera y funciona bien allí, pero por razones de seguridad no quiero usarla. Estoy usando Node.js versión 16.15.1

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Uh, si el tiempo de espera en spawn no funciona... puede usar la lógica setTimeout para matar el programa que tendría su límite de tiempo excedido

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!