Skip to content

Instantly share code, notes, and snippets.

@akirattii
Last active December 27, 2017 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akirattii/ebd137fbe54d1665f5980c7104627772 to your computer and use it in GitHub Desktop.
Save akirattii/ebd137fbe54d1665f5980c7104627772 to your computer and use it in GitHub Desktop.
NodeJS: SharedMemory: Memory sharing on every worker of cluster
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
//
// ** SharedMemory which is shared among every worker:
//
// dont use `var`
SharedMemory = {
// shared data:
data: null,
// used by a worker to update shared data:
update: function(data) {
cluster.worker.send(data);
},
};
//
// ** Start workers:
//
if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
Object.keys(cluster.workers).forEach(function(id) {
console.log("id", id);
let worker = cluster.workers[id];
worker.on('message', function(data) {
console.log("[cluster] onMessage:", data);
SharedMemory.data = data;
// sends the data updated to every worker:
Object.keys(cluster.workers).forEach(id => {
cluster.workers[id].send(SharedMemory.data);
});
});
});
} else {
//change this line to Your Node.js app entry point.
require("./server.js");
// receives data from master thread:
process.on('message', function(data) {
console.log(`[cluster] update SharedMemory.data of process:${process.pid}:`, data);
// update:
SharedMemory.data = data;
});
}
// ### Start:
// $ node cluster.js
const express = require("express");
const app = express();
const cluster = require('cluster');
const myname = "worker" + cluster.worker.id;
app.get('/', function(req, res) {
// SharedMemory is defined as global vars on cluster.js:
console.log(`[${req.path}] Get SharedMemory.data:`, SharedMemory.data);
res.end(JSON.stringify(SharedMemory.data));
});
app.get('/update', function(req, res) {
let data = { msg: "message from " + myname };
console.log(`[${req.path}] SharedMemory.update:`, data);
SharedMemory.update(data);
res.end(JSON.stringify(data));
});
app.listen(3000, function() {
console.log("Running at PORT 3000");
});
/*
## Usage
$ node cluster.js
## update shared data:
http://localhost:3000/update
## view current shared data:
http://localhost:3000/update
## Console log sample
id 1
id 2
id 3
id 4
Running at PORT 3000
Running at PORT 3000
Running at PORT 3000
Running at PORT 3000
[/] Get SharedMemory.data: null
[/] Get SharedMemory.data: null
[/] Get SharedMemory.data: null
[/update] SharedMemory.update: { msg: 'message from worker3' }
[cluster] onMessage: { msg: 'message from worker3' }
[cluster] update SharedMemory.data of process:7325: { msg: 'message from worker3' }
[cluster] update SharedMemory.data of process:7320: { msg: 'message from worker3' }
[cluster] update SharedMemory.data of process:7326: { msg: 'message from worker3' }
[cluster] update SharedMemory.data of process:7327: { msg: 'message from worker3' }
[/] Get SharedMemory.data: { msg: 'message from worker3' }
[/update] SharedMemory.update: { msg: 'message from worker2' }
[cluster] onMessage: { msg: 'message from worker2' }
[cluster] update SharedMemory.data of process:7326: { msg: 'message from worker2' }
[cluster] update SharedMemory.data of process:7320: { msg: 'message from worker2' }
[cluster] update SharedMemory.data of process:7325: { msg: 'message from worker2' }
[cluster] update SharedMemory.data of process:7327: { msg: 'message from worker2' }
[/] Get SharedMemory.data: { msg: 'message from worker2' }
[/] Get SharedMemory.data: { msg: 'message from worker2' }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment