Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grasping the Node JS alternative to multithreading

People also ask

CAN node js be used as a multithreaded?

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.

How does node js work asynchronously without multithreading?

Node is multithreaded. The main event loop is single-threaded by nature. But the I/O is run on separate threads/processes, because the I/O APIs in Node. js is asynchronous/non-blocking by design, in order to accommodate the singlethreaded event loop.

Is there an alternative to node js?

AngularJS, PHP, Python, JavaScript, and React are the most popular alternatives and competitors to Node. js.

Is node JS really single threaded?

No. NodeJs is not single threaded. The NodeJs event loop operates on a single thread yes, but the async blocking operations are delegated to separate worker threads. These threads notify the main thread when they are done processing.


Pretty much correct, yes. The node.js server has an internal thread pool so it can perform blocking operations and notify the main thread with a callback or event when things complete.

So I imagine that it will make limited use of another core for the thread pool, for example if you do a non-blocking file system read this is likely implemented by telling a thread from the thread pool to perform a read and set a callback when it's done which means that the read could be happening on a different thread/core while the main node.js program is doing something else.

But from a node.js point of view, it's entirely single threaded and won't directly use more than one core.


Yes, I'd say that your understanding is entirely correct. This article (archived) explains the rationale behind this design quite well. This is probably the most important paragraph:

Apache is multithreaded: it spawns a thread per request (or process, it depends on the conf). You can see how that overhead eats up memory as the number of concurrent connections increases and more threads are needed to serve multiple simulataneous clients. Nginx and Node.js are not multithreaded, because threads and processes carry a heavy memory cost. They are single-threaded, but event-based. This eliminates the overhead created by thousands of threads/processes by handling many connections in a single thread.


Even if this is an old thread, I thought, that I would share with an idea, how to utilize more that one core in Node.JS app. As Nuray Altin have mentioned - JXcore can do that.

Simple example:

var method = function () {
    console.log("this is message from thread no", process.threadId);
};

jxcore.tasks.runOnThread(0, method);
jxcore.tasks.runOnThread(1, method);

// this is message from thread no 1
// this is message from thread no 0

By default there are two threads (you can change it with jxcore.tasks.setThreadCount())

Of course there is much more that you can do with tasks. The docs are here.

Few articles on that subject:

  • How multithreaded JXcore applications benefit from multiple cores.
  • How to turn your existing application into a multithreaded one with a few lines of code!

Since this question asked almost 2 years ago. Things are getting different or there are alternative approaches to multithreading problem on Node.JS

According to below blog post, using the incoming 'task' extension, some can benefit from other available cores directly.

http://oguzbastemur.blogspot.com/2013/12/multithread-nodejs.html