Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Threads in Node 10.5.0: A Practical Introduction

DZone's Guide to

Threads in Node 10.5.0: A Practical Introduction

The latest version of Node.js has introduced threads to this already powerful backend framework. Read on to get an overview of how to use this new feature.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

This time around, we’re requesting the homepage for Google.com and, at the same time, sorting a randomly generated array of 1 million numbers. This is going to take a few seconds, so it’s perfect for us to show how well this behaves. We’re also going to measure the time it takes for the worker thread to perform the sorting and we’re going to send that value (along with the first sorted value) to the main thread, where we’ll display the results.A few days ago, version 10.5.0 of Node.js was released and one of the main features it contained was the addition of initial (and experimental) thread support.

This is interesting, especially coming from a language that’s always prided itself on not needing threads thanks to it’s fantastic async I/O. So why would we need threads in Node?

The quick and simple answer is: to have it excel in the only area where Node has suffered in the past, dealing with heavy CPU intensive computations. This is mainly why Node.js is not strong in areas such as AI, Machine Learning, Data Science and the like. There are a lot of efforts in progress to solve that, but we’re still not as performant as when deploying microservices, for instance.

So I’m going to try and simplify the technical documentation provided by the initial PR and the official docs into a more practical and simple set of examples. Hopefully, that’ll be enough to get you started.

So How Do We Use the New Threads Module?

To start with, you’ll be requiring the module called “worker_threads.”

Note that this will only work if you use the --experimental-worker flag when executing the script, otherwise the module will not be found.

Notice how the flag refers to workers and not threads - this is how they’re going to be referenced throughout the documentation: worker threads or simply workers.

If you’ve used multi-processing in the past, you’ll see a lot of similarities with this approach, but if you haven’t, don’t worry, I’ll explain as much as I can.

What Can You Do With Them?

Worker threads are meant, like I mentioned before, for CPU intensive tasks, using them for I/O would be a waste of resources, since, according to the official documentation, the internal mechanism provided by Node to handle async I/O is much more efficient than using a worker thread for that, so… don’t bother.

Let’s start with a simple example of how you would go about creating a worker and using it.

Example 1:

const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');

let currentVal = 0;
let intervals = [100,1000, 500]

function counter(id, i){
console.log("[", id, "]", i)
return i;
}

if(isMainThread) {
console.log("this is the main thread")
for(let i = 0; i < 2; i++) {
let w = new Worker(__filename, {workerData: i});
}

setInterval((a) => currentVal = counter(a,currentVal + 1), intervals[2], "MainThread", currentVal);
} else {

console.log("this isn't")

setInterval((a) => currentVal = counter(a,currentVal + 1), intervals[workerData], workerData, currentVal);

}

The above example will simply output a set of lines showing incremental counters, which will increase their values using different speeds.

The output of the above piece of code

Let’s break it down:

  1. The code inside the IF statement creates 2 worker threads, the code for them is taken from the same file, due to the __filename parameter passed. Workers need the full path to the files right now, they can’t handle relative paths, so that is why this value is used.
  2. The 2 workers are sent a value as a global parameter, in the form of the workerData attribute you see as part of the second argument. That value can then be accessed through a constant with the same name (see how the constant is created in the first line of the file and used later on in the last line).

This example is one of the most basic things you can do with this module, but it’s not really that fun, is it? Let’s look at another example.

Example 2: Actually Doing Something

Let’s try now to do some “heavy” computation while, at the same time, doing some async stuff in the main thread.

const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
const request = require("request");


if(isMainThread) {
console.log("This is the main thread")

let w = new Worker(__filename, {workerData: null});
w.on('message', (msg) => { //A message from the worker!
console.log("First value is: ", msg.val);
console.log("Took: ", (msg.timeDiff / 1000), " seconds");
})
w.on('error', console.error);
w.on('exit', (code) => {
if(code != 0)
      console.error(new Error(`Worker stopped with exit code ${code}`))
   });

request.get('http://www.google.com', (err, resp) => {
if(err) {
return console.error(err);
}
console.log("Total bytes received: ", resp.body.length);
})

} else { //the worker's code

function random(min, max) {
return Math.random() * (max - min) + min
}

const sorter = require("./test2-worker");

const start = Date.now()
let bigList = Array(1000000).fill().map( (_) => random(1,10000))

sorter.sort(bigList);
parentPort.postMessage({ val: sorter.firstValue, timeDiff: Date.now() - start});

}

This time around, we’re requesting the homepage for Google.com and, at the same time, sorting a randomly generated array of 1 million numbers. This is going to take a few seconds, so it’s perfect for us to show how well this behaves. We’re also going to measure the time it takes for the worker thread to perform the sorting and we’re going to send that value (along with the first sorted value) to the main thread, where we’ll display the results.

Here is the output from example #2

The main takeaway from this example is the communication between threads.

Workers can receive messages in the main thread through the on method. The events we can listen to are the ones shown in the code. The message event is triggered whenever we send a message from the actual thread using the parentPort.postMessage method. You could also send a message to the thread’s code using the same method, on your worker instance and catching them using the parentPortobject.

In case you’re wondering, the code for the helper module I used is here, although there is nothing noteworthy about it.

Let’s now look at a very similar example, but with some cleaner code, giving you a final idea of how you could structure your worker thread’s code.

Example 3: Bringing it All Together

As a final example, I’m going to stick to the same functionality, but showing you how you could clean it up a bit and have a more maintainable version.

const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
const request = require("request");

function startWorker(path, cb) {
let w = new Worker(path, {workerData: null});
w.on('message', (msg) => {
cb(null, msg)
})
w.on('error', cb);
w.on('exit', (code) => {
if(code != 0)
      console.error(new Error(`Worker stopped with exit code ${code}`))
   });
return w;
}

console.log("this is the main thread")

let myWorker = startWorker(__dirname + '/workerCode.js', (err, result) => {
if(err) return console.error(err);
console.log("[[Heavy computation function finished]]")
console.log("First value is: ", result.val);
console.log("Took: ", (result.timeDiff / 1000), " seconds");
})

const start = Date.now();
request.get('http://www.google.com', (err, resp) => {
if(err) {
return console.error(err);
}
console.log("Total bytes received: ", resp.body.length);
//myWorker.postMessage({finished: true, timeDiff: Date.now() - start}) //you could send messages to your workers like this
}) 

And your thread code can be inside another file, such as:

const {  parentPort } = require('worker_threads');

function random(min, max) {
return Math.random() * (max - min) + min
}

const sorter = require("./test2-worker");

const start = Date.now()
let bigList = Array(1000000).fill().map( (_) => random(1,10000))

/**
//you can receive messages from the main thread this way:
parentPort.on('message', (msg) => {
console.log("Main thread finished on: ", (msg.timeDiff / 1000), " seconds...");
})
*/

sorter.sort(bigList);
parentPort.postMessage({ val: sorter.firstValue, timeDiff: Date.now() - start});

Breaking this one down, we see:

  1. Main thread and worker threads now have their code inside different files. This is easier to maintain and extend.
  2. The startWorkerfunction returns the new instance, allowing you to later send messages to it if you so wanted.
  3. You no longer need to worry if your main thread’s code is actually the main thread (we removed the main IF statement).
  4. You can see in the worker’s code how you would receive a message from the main thread, allowing for a two-way asynchronous communication.

That is going to be it for this article, I hope you got enough to understand how to get started playing around with this new module. Remember that:

  1. This is still highly experimental and the things explained here can change in future releases.
  2. Go and read the PR comments and docs, as there is more information about this in there, I just focused on the basic steps to get you going.
  3. Have fun! Play around, report bugs, and suggest improvements; this is just starting!

That's it for now, let me know what your thoughts are regarding this new module and how you're planning on using it once it's actually stable. Also, if you want more content like this or are looking for Node.js related books, please visit my site at www.fernandodoglio.com

Thanks for reading!

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
threads ,javascript ,node.js ,web dev ,backend development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}