microjob icon indicating copy to clipboard operation
microjob copied to clipboard

Confused about starting multiple worker threads

Open andycorm opened this issue 3 years ago • 1 comments

I apologize, this is really a question and not an issue, but I can't seem to figure out if it's possible to spawn more than one worker thread using microjob.

I'm trying something like the following:

import { job, start, stop } from 'microjob';

(async () => {

  try {
    // start the worker pool
    await start();

    // this function will be executed in another thread
    const jobA = job(() => {
      console.log(`Worker ${process.pid} started`);
    });

    // this function should be executed in yet another thread
    const jobB = job(() => {
      console.log(`Worker ${process.pid} started`);
    });

    await Promise.all(jobA, jobB);

  } catch (err) {
    console.error(err);
  } finally {
    // shutdown worker pool
    await stop();
  }
})();

Every time I try something like this though, both worker threads console.log the exact same pid value. Am I doing something wrong? Or is this just a limitation with microjob?

andycorm avatar Jan 18 '22 00:01 andycorm

@andycorm microjob is using worker_threads internally so it does not deal with multiple processes. You may be confusing worker_threads with child_process

dillonstreator avatar May 24 '22 17:05 dillonstreator