Node.js on multi-core machines

前端 未结 15 1276
猫巷女王i
猫巷女王i 2020-11-22 07:46

Node.js looks interesting, BUT I must miss something - isn\'t Node.js tuned only to run on a single process and thread?

Then how does it scale for m

相关标签:
15条回答
  • 2020-11-22 08:16

    It's also possible to design the web-service as several stand alone servers that listen to unix sockets, so that you can push functions like data processing into seperate processes.

    This is similar to most scrpting/database web server architectures where a cgi process handles business logic and then pushes and pulls the data via a unix socket to a database.

    the difference being that the data processing is written as a node webserver listening on a port.

    it's more complex but ultimately its where multi-core development has to go. a multiprocess architecture using multiple components for each web request.

    0 讨论(0)
  • 2020-11-22 08:24

    You may run your node.js application on multiple cores by using the cluster module on combination with os module which may be used to detect how many CPUs you have.

    For example let's imagine that you have a server module that runs simple http server on the backend and you want to run it for several CPUs:

    // Dependencies.
    const server = require('./lib/server'); // This is our custom server module.
    const cluster = require('cluster');
    const os = require('os');
    
     // If we're on the master thread start the forks.
    if (cluster.isMaster) {
      // Fork the process.
      for (let i = 0; i < os.cpus().length; i++) {
        cluster.fork();
      }
    } else {
      // If we're not on the master thread start the server.
      server.init();
    }

    0 讨论(0)
  • 2020-11-22 08:25

    The new kid on the block here is LearnBoost's "Up".

    It provides "Zero-downtime reloads" and additionally creates multiple workers (by default the number of CPUs, but it is configurable) to provide the best of all Worlds.

    It is new, but seems to be pretty stable, and I'm using it happily in one of my current projects.

    0 讨论(0)
  • 2020-11-22 08:27

    Future version of node will allow you to fork a process and pass messages to it and Ryan has stated he wants to find some way to also share file handlers, so it won't be a straight forward Web Worker implementation.

    At this time there is not an easy solution for this but it's still very early and node is one of the fastest moving open source projects I've ever seen so expect something awesome in the near future.

    0 讨论(0)
  • 2020-11-22 08:28

    You can use cluster module. Check this.

    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);
    }
    
    0 讨论(0)
  • 2020-11-22 08:28

    Multi-node harnesses all the cores that you may have.
    Have a look at http://github.com/kriszyp/multi-node.

    For simpler needs, you can start up multiple copies of node on different port numbers and put a load balancer in front of them.

    0 讨论(0)
提交回复
热议问题