only one worker_threads running when i spawned two in nodejs

184 Views Asked by At

I have this file analyze-caption-job.ts

async function analyzeCaptionJob(): Promise<void> {
  while (true) {
    console.log("analyzing!");
    await new Promise(f => setTimeout(f, 1000));
  }

}

analyzeCaptionJob();

And youtube-metadata-job.ts


async function youtubeMetadataJob(): Promise<void> {
  while (true) {
    console.info("youtubing!");
    await new Promise(f => setTimeout(f, 1000));
  }
}

youtubeMetadataJob();

and a main file cron.ts

import path from "path";

import { Worker } from 'worker_threads';

const runWorkers = async () => {
  const worker = new Worker(path.join(__dirname, 'jobs') + '/analyze-caption-job.ts', {});
    
  const worker2 = new Worker(path.join(__dirname, 'jobs') + '/youtube-metadata-job.ts', {});
    
}

runWorkers();

When I run it with npx ts-node-dev cron.ts,

The output is:

youtubing!
youtubing!
youtubing!
youtubing!
youtubing!

The analyzing! is not printing at all.

However, if I add a 5 sec "pause/sleep" in between the worker initializations. It's fixed. Why is this happening?

import path from "path";

import { Worker } from 'worker_threads';

const runWorkers = async () => {
  const worker = new Worker(path.join(__dirname, 'jobs') + '/analyze-caption-job.ts', {});

   
+  await new Promise(f => setTimeout(f, 5000));
  
  const worker2 = new Worker(path.join(__dirname, 'jobs') + '/youtube-metadata-job.ts', {});
    
}

runWorkers();

Output:

youtubing!
analyzing!
youtubing!
analyzing!
youtubing!
analyzing!
1

There are 1 best solutions below

0
Andranik Arakelyan On

Yeah, I saw the exact same problems, and I don't know solution, when you are running code via ts-node-dev. But I found out, that problem is with ts-node-dev, because the same code with "clean" nodejs code is works fine. Yeah, there are problems with file extension ( when we want to use *.ts files with worker threads ), but anyway, it works.

import path from "path";

import { Worker } from 'worker_threads';

const runWorkers = async () => {
  const worker = new Worker(path.join(__dirname, 'analyze-caption-job.js'));
  const worker2 = new Worker(path.join(__dirname, 'youtube-metadata-job.js'));

}

runWorkers();

Scripts

npx tsc # to build
node dist/jobs/cron.js # to run