SUCHMOKUO/node-worker-threads-pool

如果 worker 里面跑的是死循环的话,应该是无法超时的吧

qile222 opened this issue · 3 comments

单个线程死循环了没有机会来执行计时器的回调,所以这个 race 是不是放到主线程里面去,然后用 worker.terminate() 去终止
image

@lolBig 是的,本来就是在主线程跑的

好厉害,搜了半天发现中文了。有个类似的问题,一块在这咨询下了哈。 (为了globalization,还是英语提问吧)
@SUCHMOKUO ,
I have a relate issue,
Issue :
timeout can't terminate threads when there's setInterval/setTimeout in worker threads.

  1. If I enabled next ①, then I can recevie timeout as expected,
  2. if I disabled next ①, there's no timeout , but worker indeed is running (as you can see ②)

Do you know how to force stop threads after timeout ?

Demo:
//hello module:

exports.world = function(value) {
  console.info("[hello] input:%d" , value);
  const process = require('process');

  setInterval( ()=>{ // **②**
    console.log("[hello]thread exist :%d", process.uptime());
    //while(1);
  },2000)

  //while(1); //  **①**   (only for test)

  return 1
}

// main threads:

const { DynamicPool, isTimeoutError } = require('node-worker-threads-pool');
const dynamicPool = new DynamicPool(4);
let mymodule = require('./service/hello')

dynamicPool.exec({
  task: mymodule['world'],
  param: 1,
  timeout: 5000
}).then( (result,) => {
  console.log("hello return:"+result);
}).catch ( (err) => {
  if (isTimeoutError(err)) {
    console.log("---> task timeout !")
  } 
})