Skip to content

Instantly share code, notes, and snippets.

@wjramos
Last active August 27, 2017 04:09
Show Gist options
  • Save wjramos/4ca41bee207c998354e1c845d001ced1 to your computer and use it in GitHub Desktop.
Save wjramos/4ca41bee207c998354e1c845d001ced1 to your computer and use it in GitHub Desktop.
import cluster from 'cluster';
import { cpus } from 'os';
export function forkProcess(proc) {
const cores = cpus();
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running, forking process onto ${cores.length} cores`);
cores.forEach(core => cluster.fork());
cluster.on('exit', worker => console.log(`worker ${worker.process.pid} died`));
} else {
console.log(`App started on worker ${process.pid}`);
proc();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment