0
聽我有一些很難理解這段代碼:集羣的NodeJS模塊 - 它是如何工作的,而在插座
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
我不明白家長和工作人員之間的關係。父進程生成子進程並等待它們,但工作負載如何分配(子進程)
如何將多進程綁定到子進程中的同一個端口?