Alireza
Alireza

Reputation: 6848

Node.js is single threaded does this mean we cannot run multiple Node.js in different threads?

I've read in an article that Node.js is single threaded. The question is what if we run multiple Node.js files in different ports? Do they have their own thread or all of them will be under the main Node.js thread? Could someone shed someone light on the subject I'm now on the dark side.

Upvotes: 2

Views: 1101

Answers (2)

Brad
Brad

Reputation: 163262

The question is what if we run multiple Node.js files in different ports? Do they have their own thread or all of them will be under the main Node.js thread?

From your question, it sounds to me like you are actually starting up multiple processes of Node.js. In those cases, they will work like any other set of multiple processes on your system, and your OS will attempt to balance the load out between all the cores and/or CPUs.

I've read in an article that Node.js is single threaded.

This is a bit more complicated. While the V8 JavaScript engine Node.js uses does run your JavaScript in a single thread, much of the Node.js libraries call out to native code which can use as many threads as it likes. These internals of Node.js use a thread pool and multithreading for disk and network IO, among other tasks.

The applications Node.js really shines in are those that are typically IO bound. In these cases, you get much of the benefit of multithreading without having to write any code for it. For example, when you make multiple requests to disk Node.js will use multiple threads to handle the buffering and management of that data while not blocking your main JavaScript thread.

In many of my applications, I have found that I can fully utilize an 8-core box without writing any code to fire up child processes. Node's internal multithreading does all the work for me. Your mileage will vary from application to application.

I've written a different explanation on a past question you might find helpful: https://stackoverflow.com/a/19324665/362536

Upvotes: 5

TruongSinh
TruongSinh

Reputation: 4856

http://nodejs.org/api/cluster.html

A single instance of Node runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node processes to handle the load.

The cluster module allows you to easily create child processes that all share server ports.

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
} else {
  // Workers can share any TCP connection
  // In this case its a HTTP server
  http.createServer(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);
}

Which means you have to architect yourself, so if you want to listen to different port in different threads, either use cluster or child_process.

Upvotes: 1

Related Questions