cluster

一个cluster集群小demo

// 引入程序
const app = require('./app')
const cluster = require('cluster')

if (cluster.isMaster) {
    let total = require('os').cpus().length - 1
    console.log('running %d total workers', total)
    for (let i = 0; i < total; i++) {
        cluster.fork()
    }
    cluster.on('exit', (worker) => {
        console.log('worker %d died', worker.id)
        cluster.fork()
    })
} else {
    console.log('worker pid: ', process.pid)
    app.listen('9898')
}

你可能感兴趣的:(cluster)