2

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.

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

@phpGeek See the answer was something related to using multicore processors, by forking the processes. So, the are no threads involved here. Basically you can't create a thread in Node.js. But in this TruongSinh's answer explains how to take advantages of multicore by forking child_process from a master. And with port, its there in his answer child processes shares server port.
@phpGeek if you read the documentation further. short answer: no, you can only run instances on the same port but different processes (which shares nothing but parent process, yet you child processes can communicate with each others and parent)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.