Node.js 多进程

工作进程:

var http = require('http');
var port = Math.round((1 + Math.random()) * 1000);

http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World\n');
}).listen(port, '127.0.0.1');

console.log("port: ", port);


主进程:

var { fork } = require('child_process');
var cpus = require('os').cpus();

for (var i = 0; i < cpus.length; i++) {
  fork('./process_worker.js');
}

修改时间 2022-02-28

声明:本站所有文章和图片,如无特殊说明,均为原创发布,转载请注明出处。
随机推荐
WordPress 函数 add_option()、get_option() 和 update_option()
Node.js 安装第三方模块
ES6 Promise 和 async/await 教程
JavaScript 数据类型和变量
WordPress 增强编辑器功能
Node.js querystring 模块
WordPress 主题激活和取消激活钩子
CSS 盒模型