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 发送邮件
HTML input datetime 日期时间选择器
WordPress 插件开发
Land APP 小程序
WordPress 分类添加自定义字段
JavaScript history对象
Express 使用 body-parser 处理 HTTP 请求
JavaScript 私有方法和私有属性