Does Node.js support parallelism?

I was looking more for the difference between concurrency and parallelism. I stumbled upon the YouTube conversation Rob Pike gave about the differences between concurrency and parallelism. His conversation was in the context of the Google Go language. From what I understand, concurrency is a way to manage several things, but parallelism is the physical execution of several things at the same time.

From what I also understand, Node.js works as a single thread process. So, although Node supports concurrency with the way it implements callbacks, etc., does it have the ability to execute tasks in parallel? Is it possible to configure multiple threads on separate processors?

+11
source share
3 answers

A node can support Concurrency via the Cluster or child_process modules packaged in the Core Nodejs API. Both of these modules create additional processes, not additional threads.

Streams are created and managed, sometimes in a pool, “under the hood” using libuv , which node implements and uses to perform asynchronous operations, such as reading from a file. You cannot explicitly create a stream from your executable Javascript. You can read more about libuv at the following resources.

Also, this is a great thread and pool question, which has a lot of detail.

Cluster used to distribute the Server workload across several cores and at the same time allows them to share the port. However, Cluster uses child_process.fork() under the hood and communicates via interprocess communication . You can read more about Cluster in Nodejs Core API Docs .

child_process offers several different ways to parallelize work through exec() , spawn() or fork() . A parent process can interact with a child process using interprocess communication through channels.

In essence, Node wants you to use the event loop and leave libuv flow libuv . This is why it is so easy to create code that usually requires blocking measures and thread safety. If you must do work that requires hard work or a significant amount of blocking (synchronous) work, then you can use child_process to take off this work. If you need to scale your web application by core, use Cluster .

+8
source

If I'm not mistaken, you are looking for something like multithreading and / or computing several things at once.

Multiple execution at once
SIMD begins to search its way in browsers, and the Node implementation is not far behind. Take a look at node-simd or MDN SIMD (browsers) . SIMD is still experimental and only works on supported processors at the moment (obviously, since not all processors have this functionality). It performs several actions at once, an example of comparing regular JS with SIMD JS is:

 // 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
As for multithreading, webworkers already exist in the browser environment. This has been ported to Node in full compliance with the HTML specification, as shown in this repository .
Here's a good explanation as to why webworkers were even ported to Node, primarily because you can already run child processes on other threads and other processors with the child_process module. node-webworker is a great module, as each process runs inside its context and in its own node process, so it is really multithreaded with node.

So, in order to start different processes on different processors, you must either accept node-webworker or use child_process to create other threads to simultaneously perform separate actions 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 :)

+1
source

The execution of several threads in separate processors on the same host computer can be achieved by using the proprietary Node os and cluster modules. Below 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) 
0
source

All Articles