Use setImmediate to enqueue tasks sequentially so the work is delegated to multiple threads.

This commit is contained in:
Dillon Kearns 2021-07-20 14:47:58 -07:00
parent 951d57a12a
commit eef18eee93

View File

@ -377,7 +377,12 @@ async function start(options) {
readyThreads.forEach((readyThread) => {
const startTask = threadReadyQueue.shift();
if (startTask) {
startTask(readyThread);
// if we don't use setImmediate here, the remaining work will be done sequentially by a single worker
// using setImmediate delegates a ready thread to each pending task until it runs out of ready workers
// so the delegation is done sequentially, and the actual work is then executed
setImmediate(() => {
startTask(readyThread);
});
}
});
}