Home > Web Front-end > JS Tutorial > NodeJS parent process and child process resource sharing principle and implementation method

NodeJS parent process and child process resource sharing principle and implementation method

亚连
Release: 2018-05-29 17:40:23
Original
1667 people have browsed it

This article mainly introduces the principle and implementation method of resource sharing between the parent process and the child process of NodeJS, and analyzes the related operating techniques of nodejs based on the cluster module to realize resource sharing between the parent process and the child process in the form of examples. Friends in need can refer to the following

The examples in this article describe the principle and implementation method of resource sharing between NodeJS parent process and child process. Share it with everyone for your reference, the details are as follows:

Experimental goal: Realize resource sharing between parent process and child process

Use module: cluster

Introduction: Establish a node cluster and implement multi-process, using child_process IPC solves multi-core utilization and improves performance.

Principle:

1 Multi-process architecture of Master-worker master-slave mode

2 fork() Copy the process to make full use of CPU resources (determined by the number of cores)

3 Each process has its own area. If operations are performed in their respective areas, the resources are not shared. By listening to message events and sending, message passing is achieved to achieve the effect of resource sharing

4 globalDataError is the wrong resource sharing method, and globalDataSuccess is the correct resource sharing method.

Implementation code:

var cluster = require('cluster');
var cpus = require('os').cpus();
// 传递的事件名
var triggerEvent = {
    inc: 'inc',
    dec: 'dec'
}
// 错误的数据共享方式
var globalDataError = 0;
if (cluster.isMaster) {
  // 正确的数据共享方式
  var globalDataSuccess = 0;
  globalDataError++;
  // 启动多个进程,取决于内核数
  for (var i = 0; i < cpus.length; i++) {
    var worker = cluster.fork();
    worker.on(&#39;message&#39;, function(msg) {
      switch (msg) {
        case triggerEvent.inc:
          globalDataSuccess++;
          console.log(&#39;globalDataSuccess = &#39;, globalDataSuccess);
          break;
        case triggerEvent.dec:
          globalDataSuccess--;
          console.log(&#39;globalDataSuccess = &#39;, globalDataSuccess);
          break;
      }
    });
  }
  console.log(&#39;Master globalDataError = &#39;, globalDataError);
} else {
  globalDataError++;
  console.log(&#39;Worker globalDataError = &#39;, globalDataError);
  process.send(triggerEvent.dec);
  process.send(triggerEvent.inc);
}
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Solution to the flashing problem of vue page loading

A brief discussion on the problem of js obtaining the ModelAndView value

The problem and solution of {{}} flickering when vue renders

The above is the detailed content of NodeJS parent process and child process resource sharing principle and implementation method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template