tinylibs / tinypool

🧵 A minimal and tiny Node.js Worker Thread Pool implementation (38KB)

Home Page:https://npmjs.com/package/tinypool

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`isolateWorkers` option can result in not utilizing all CPUs

overlookmotel opened this issue · comments

If isolateWorkers option is used, the number of tasks running in parallel can fall to minThreads. If minThreads is 0, it stops entirely.

I think this happens because of the code here which handles when a task completes:

tinypool/src/index.ts

Lines 843 to 847 in c5705f6

// When `isolateWorkers` is enabled, remove the worker after task is finished
if (this.options.isolateWorkers && taskInfo.workerInfo) {
this._removeWorker(taskInfo.workerInfo)
this._ensureMinimumWorkers()
}

this._ensureMinimumWorkers() will launch a fresh worker if number of current threads is less than minThreads. But if minThreads is lower than maxThreads, no new thread is created. The rest of the task queue is processed using only minThreads threads.

If minThreads is 0, processing of the queue grinds to a halt entirely.

Nice catch, let me know if you're interested in working on this.

I'm really sorry, but no I can't work on this.

To explain: For my use case, setting minThreads and maxThreads to the same number works fine, so that solves the problem for me.

My job is nothing to do with software development, so only time I have to do coding is in my quite limited spare time. I have a quite sizeable open source project on the go (Livepack) but keep getting sidetracked working on other OSS projects, and am struggling to make headway. So I need to stop!

You probably don't need this explanation, but I well understand the time-poor situation of many OSS maintainers, and didn't want to be just another of those "Hey your package doesn't work. Fix it for me right now!" people.

I hope the bug report is at least useful, even if I'm afraid I can't contribute a fix myself.

No need to be sorry, I get it. Thank you so much, the issue is really helpful. I've got so many stuff on my plate and at the same time I'm not well-sponsored, that's one of the issues.
But soon I'll solve this issue if possible.

Vitest would benefit greatly, if this is fixed.

@sheremet-va Out of interest, why does vitest need to isolate workers?

@sheremet-va Out of interest, why does vitest need to isolate workers?

This option was actually added for Vitest initially. Vitest uses isolated workers to run tests in isolation.