Worker thread initation
Opened this issue · 8 comments
Now the threads are in the pool as soon as they are created.
In my project the worker threads spend long time on initation and I don't want them to be in the pool before they are ready to serve.
Like 'lazy initialization' ? the worker won't be created until a task execution request come?
就是线程要先加载完数据,进入ready状态再开始接受任务请求
现在是线程刚创建就开始接收请求了,本身其实还在执行加载
默认的算法好像是最新的线程优先级会比较高,导致其实有多个线程ready的情况下,刚创建,还没有加载完数据的线程接收到任务了,这时要等待这个线程加载完成任务才会开始
@mt4c 这里的实现其实是内部有一个任务队列,所有的任务执行请求都会先将任务添加进队列中,然后等待一个线程来取走它。如果是已经有空闲的线程,那么任务会被立即取走执行,反之则在队列中等待。并且所有的线程都只会在 ready 之后才会从队列取任务执行。而且这里并没有特别的优先级策略,哪个线程先 ready 就哪个线程去任务队列头部取任务执行,所以我理解你的问题或许是其他的原因导致的?
我这里线程代码的结构是这样的
init() // 线程初始化
parentPort.on('message', ()=> {
doJob() // 业务逻辑
})
之前由于某些原因,doJob会抛出异常,导致线程挂掉,此时系统会开启一个新线程。
当有新的任务时,线程池会先用这个新线程执行,而实际上这个线程还在init的阶段,从而导致任务一段时间内被挂起
后来我改成把异常抛给主线程,避免了这个问题
但是我还是希望线程在init的时候标记成未ready,不要接收任务,让其它ready的线程执行
Can you guys please suggest a book to learn Chinese (JK) :)? Would appreciate it if we keep the discussion in English. This seems to be a nice library and I would certainly like to contribute if I know what's being talked about.
@thatisuday Hi. We're just talking about a new feature that allow threads itself to decide when to be ready to be picked up by the main thread to execute tasks rather than decide by the main thread.