简体   繁体   中英

nodejs worker_threads How to understand that all workers have completed work

Began to study worker_threads in nodejs
After completion of work, the last worker must finish the script
process.exit();

my index.js

const { Worker } = require("worker_threads");
const logUpdate = require("log-update");
const threads = 100;
const someData = 'some data';

let names = [...Array(threads)].fill(0);
for (let i = 0; i < threads; i++) {

    const port = new Worker(require.resolve("./worker.js"), {
        workerData: { someData, i }
    });

    port.on("message", (data) => handleMessage(data, i));
    port.on("error", (e) => console.log(e));
    port.on("exit", (code) => console.log(`Exit code: ${code}`));
}

function handleMessage(_, index) {
    names[index]++;
    logUpdate(names.map((status, i) => `Thread ${i}: ${status}  ${_}`).join("\n"));
}

worker.js

const { parentPort, workerData } = require("worker_threads" )
const { someData, i } = workerData;

(async () => {
    parentPort.postMessage( `worker start ${i} some data ${someData}`);
    process.exit();
})();

Now workers are created and work out, but after they are completed, the script does not complete its work

From whatever knowledge I have, I understand that if there is no error caused in the worker until its exit, then the return code is 0, otherwise 1 (automatically). Look at process exit codes .

So when you do process.exit() at that time you can pass the code as an argument. (or not have process.exit() at all).

Generally what I have seen, and do is:

  1. Wrap the worker creation in a Promise returning function.
    NOTE:

    • worker.terminate(): terminates the worker thread.
    • (Personal choice) I choose to do parentPort.postMessage(JSON.stringify(true)) if I complete my task in my worker thread. Hence
      worker.on('message', message => {if (message === true) {worker.terminate();resolve(true);} else {console.log(message)}});
      in the main thread.
  2. Whenever I have to create threads for repetitive task-delegations. I use.map function on the array. the function inside returns the same return value as that from my worker creator (Promised returning) function. Hence eventually I map the array into array of promises.

  3. Then I put this array of promises into Promise.all().
  4. Then I check for returned values of Promise.all()


Checkout:

  1. My Gist (as an example for Promise.all() ).
  2. Medium (Just for worker_thread basics. An Awesome article)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM