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?
At a high level, Node. js falls into the category of concurrent computation. This is a direct result of the single-threaded event loop being the backbone of a Node. js application.
js uses a single-threaded event loop architecture. It is also asynchronous in nature. These are the two main reasons why node. js can handle multiple concurrent requests easily and why it becomes an obvious choice for the development of such applications.
You can achieve multithreading by generating multiple nodes or Node. js V8 engines which in isolation are single-threaded. It is still correct to say Node. js is not multi-threaded.
Parallel. js is a tiny library for multi-core processing in Javascript. It was created to take full advantage of the ever-maturing web-workers API. Javascript is fast, no doubt, but lacks the parallel computing capabilites of its peer languages due to its single-threaded computing model.
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
libuv
API Docslibuv
booklibuv
(part of libuv
book)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
.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With