wilk / microjob

A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.
https://wilk.github.io/microjob/
MIT License
2.02k stars 47 forks source link

Confused about starting multiple worker threads #76

Open andycorm opened 2 years ago

andycorm commented 2 years ago

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?

dillonstreator commented 2 years ago

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