var Hub = require('cluster-hub'); var cluster = require('cluster'); var hub = new Hub(cluster); if (cluster.isMaster) { var worker = cluster.fork(); hub.on('master-to-master', function (data) { console.log('master-to-master received'); }); hub.on('worker-to-master', function (data) { console.log('worker-to-master received'); }); hub.sendToMaster('master-to-master', 1); hub.sendToWorker(worker, 'master-to-worker'); } else { hub.on('master-to-worker', function () { console.log('master-to-worker received');; process.exit(); }); hub.sendToMaster('worker-to-master', 2); }
var Hub = require('cluster-hub'); var cluster = require('cluster'); var hub = new Hub(cluster); if (cluster.isMaster) { // in master process hub.on('sum', function (data, sender, callback) { callback(null, data.a + data.b); }); var worker = cluster.fork(); } else { //in worker process hub.requestMaster('sum', {a: 1, b:2}, function (err, sum) { console.log('Sum in worker: ' + sum); process.exit(); }); }
var Hub = require('cluster-hub'); var cluster = require('cluster'); var hub = new Hub(cluster); if (cluster.isMaster) { var worker = cluster.fork(); hub.lock('foo', function (unlock) { console.log('foo lock in master'); setTimeout(unlock, 1000); }); } else { hub.lock('foo', function (unlock) { console.log('foo lock in worker 1'); setTimeout(unlock, 500); }); hub.lock('bar', function (unlock) { console.log('bar lock in worker'); unlock(); }) hub.lock('foo', function (unlock) { console.log('second foo lock in worker'); unlock(); process.exit(); }) }
Source: https://habr.com/ru/post/191192/
All Articles