Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker thread initation #34

Open
mt4c opened this issue Oct 20, 2021 · 8 comments
Open

Worker thread initation #34

mt4c opened this issue Oct 20, 2021 · 8 comments
Labels
enhancement New feature or request

Comments

@mt4c
Copy link

mt4c commented Oct 20, 2021

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.

@SUCHMOKUO
Copy link
Owner

Like 'lazy initialization' ? the worker won't be created until a task execution request come?

@mt4c
Copy link
Author

mt4c commented Nov 4, 2021

就是线程要先加载完数据,进入ready状态再开始接受任务请求

现在是线程刚创建就开始接收请求了,本身其实还在执行加载
默认的算法好像是最新的线程优先级会比较高,导致其实有多个线程ready的情况下,刚创建,还没有加载完数据的线程接收到任务了,这时要等待这个线程加载完成任务才会开始

@SUCHMOKUO
Copy link
Owner

@mt4c 这里的实现其实是内部有一个任务队列,所有的任务执行请求都会先将任务添加进队列中,然后等待一个线程来取走它。如果是已经有空闲的线程,那么任务会被立即取走执行,反之则在队列中等待。并且所有的线程都只会在 ready 之后才会从队列取任务执行。而且这里并没有特别的优先级策略,哪个线程先 ready 就哪个线程去任务队列头部取任务执行,所以我理解你的问题或许是其他的原因导致的?

@mt4c
Copy link
Author

mt4c commented Nov 8, 2021

我这里线程代码的结构是这样的

init()    // 线程初始化

parentPort.on('message', ()=> {
    doJob() // 业务逻辑
})

之前由于某些原因,doJob会抛出异常,导致线程挂掉,此时系统会开启一个新线程。
当有新的任务时,线程池会先用这个新线程执行,而实际上这个线程还在init的阶段,从而导致任务一段时间内被挂起
后来我改成把异常抛给主线程,避免了这个问题
但是我还是希望线程在init的时候标记成未ready,不要接收任务,让其它ready的线程执行

@SUCHMOKUO
Copy link
Owner

SUCHMOKUO commented Nov 8, 2021

@mt4c 听上去是个新功能 👀 比如让 worker 里的程序自己决定什么时候 ready

@mt4c
Copy link
Author

mt4c commented Nov 9, 2021

@mt4c 听上去是个新功能 👀 比如让 worker 里的程序自己决定什么时候 ready

是的

@SUCHMOKUO SUCHMOKUO assigned SUCHMOKUO and unassigned SUCHMOKUO Nov 10, 2021
@SUCHMOKUO SUCHMOKUO added the enhancement New feature or request label Nov 10, 2021
@thatisuday
Copy link

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.

@SUCHMOKUO
Copy link
Owner

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants