QAH
QAH

Reputation: 4300

Does Node.js support parallelism?

I've been looking more into the difference between concurrency and parallelism. I came upon a talk on YouTube given by Rob Pike speaking about the differences between concurrency and parallelism. His talk was in the context of the Google Go language. From what I understand, concurrency is the design of being able to handle multiple things, but parallelism is the physical execution of multiple things at the same time.

From what I also understand, Node.js runs as a single thread process. So although Node supports concurrency with the way it implements callbacks and such, does it have the ability to handle parallel execution of tasks? Can it be set up to run multiple threads on separate processors?

Upvotes: 18

Views: 16395

Answers (3)

peteb
peteb

Reputation: 19418

Node can support "Parallelism" via either the Cluster or child_process modules packaged in the Nodejs Core API. Both of these modules create additional processes and not additional threads.

Threads are created and managed, sometimes in a pool, "under-the-hood" by libuv which node implements and uses to perform the asynchronous operations such as reading from a file. You cannot explicitly create a thread from your executing Javascript. You can read more about libuv at the below resources

Also, this is great question on Threads and Pooling which has a lot of detail.

Cluster is used distribute the workload of a Server across multiple cores and still have them share a port. However, Cluster uses child_process.fork() under the hood and communicates via inter-process communication. You can read more about Cluster in the Nodejs Core API Docs.

child_process offers a couple different ways to parallelize work via exec(), spawn() or fork(). The parent process can communicate with the child process using inter-process communication via pipes.

Basically, Node wants you to leverage the event loop and leave thread management to libuv. This is why it is so easy to build code that would typically require locking and thread safety measures. If you must do work that requires heavy lifting, or a significant amount of blocking (synchronous) work, then you could use child_process to offload that work. If you need to scale out a web app across cores then use Cluster.

Upvotes: 16

Lae Kettavong
Lae Kettavong

Reputation: 399

Running multiple threads in separate processors on one host machine can be achieved by leveraging the native Node os and cluster modules. Following is a simple contrived example

const cluster = require('cluster');
const os = require('os');

((port) => {
    if (cluster.isMaster) {
        // fork the process
        os.cpus().forEach((cpu) => {
            cluster.fork();
        });

    } else {
        // if we're not in the master thread, start the HTTP server
        http.createServer((req, res) => {
            // Handle request
        }).listen(port, () => {
            console.log(`The server is listening on port ${port}`);
        });
    }
})(3000)

Upvotes: 1

If I'm not mistaken, you're looking for something along the lines of multithreading and/or computing multiple things at the same time.

Multiple executions at once
SIMD is beginning to find its way into Browsers, with Node implementations not far behind. Take a look at node-simd or MDN SIMD (browsers). SIMD is still experimental, and only works on supported CPUs at the moment (obviously, since not all CPUs have this functionality). It executes multiple things at once, an example comparing normal JS to SIMD JS would be:

// Normal addition, 4 actions are executed.
var a = [1, 2, 3, 4];
var b = [5, 6, 7, 8];
var c = [];

c[0] = a[0] + b[0];
c[1] = a[1] + b[1];
c[2] = a[2] + b[2];
c[3] = a[3] + b[3];
c; // Array[6, 8, 10, 12]

// SIMD execution - all additions are processed simultaenously through the CPU
var a = SIMD.Float32x4(1, 2, 3, 4);
var b = SIMD.Float32x4(5, 6, 7, 8);
var c = SIMD.Float32x4.add(a,b);
c; // Float32x4[6, 8, 10, 12]

Multithreading
In regards to multithreading, webworkers have existed in the Browser environment for a while now. This has been ported to Node fully according to the HTML spec as seen in this repository.
Here's a good explanation as to why webworkers were even ported to Node in the first place, since you can already run child processes in other threads and on other CPUs with the child_process module. node-webworker is a great module since each process is run within its on context, and in its own node process, so it is truly multithreading with node.

So, to run different processes on different CPUs, you would either adopt node-webworker or use child_process to spawn other threads to do separate actions at the same time with different CPU cores. node-webworker can listen for events using the familiar postMessage API, and child_process will communicate through stdin / stdout / stderr.

Hope that answers your question :)

Upvotes: 1

Related Questions