GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — node-api-cn/cluster/event_message.md at master · mouseDH/node-api-cn Expand file tree/ Copy pathevent_message.mdMore file actions 57 lines (43 loc) · 1.23 KB / Copy pathevent_message.mdFile metadata and controls57 lines (43 loc) · 1.23 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 <!-- YAML added: v0.7.0 --> * `message` {Object} * `handle` {undefined|Object} Similar to the `cluster.on('message')` event, but specific to this worker. In a worker you can also use `process.on('message')`. See [`process` event: `'message'`][]. As an example, here is a cluster that keeps count of the number of requests in the master process using the message system: ```js const cluster = require('cluster'); const http = require('http'); if (cluster.isMaster) { // Keep track of http requests let numReqs = 0; setInterval(() => { console.log(`numReqs = ${numReqs}`); }, 1000); // Count requests function messageHandler(msg) { if (msg.cmd && msg.cmd === 'notifyRequest') { numReqs += 1; } } // Start workers and listen for messages containing notifyRequest const numCPUs = require('os').cpus().length; for (let i = 0; i < numCPUs; i++) { cluster.fork(); } for (const id in cluster.workers) { cluster.workers[id].on('message', messageHandler); } } else { // Worker processes have a http server. http.Server((req, res) => { res.writeHead(200); res.end('hello world\n'); // notify master about the request process.send({ cmd: 'notifyRequest' }); }).listen(8000); } ``` |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |