worker.isDead()
添加于:v0.11.14
如果工作进程已终止(由于退出或收到信号),此函数将返回 true
。否则,它返回 false
。
import cluster from 'node:cluster'; import http from 'node:http'; import { cpus } from 'node:os'; import process from 'node:process'; const numCPUs = cpus().length; if (cluster.isPrimary) { console.log(`Primary ${process.pid} is running`); // Fork workers. for (let i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('fork', (worker) => { console.log('worker is dead:', worker.isDead()); }); cluster.on('exit', (worker, code, signal) => { console.log('worker is dead:', worker.isDead()); }); } else { // Workers can share any TCP connection. In this case, it is an HTTP server. http.createServer((req, res) => { res.writeHead(200); res.end(`Current process\n ${process.pid}`); process.kill(process.pid); }).listen(8000); }
const cluster = require('node:cluster'); const http = require('node:http'); const numCPUs = require('node:os').cpus().length; const process = require('node:process'); if (cluster.isPrimary) { console.log(`Primary ${process.pid} is running`); // Fork workers. for (let i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('fork', (worker) => { console.log('worker is dead:', worker.isDead()); }); cluster.on('exit', (worker, code, signal) => { console.log('worker is dead:', worker.isDead()); }); } else { // Workers can share any TCP connection. In this case, it is an HTTP server. http.createServer((req, res) => { res.writeHead(200); res.end(`Current process\n ${process.pid}`); process.kill(process.pid); }).listen(8000); }
相关用法
- Node.js Worker.isMainThread用法及代码示例
- Node.js Worker.exitedAfterDisconnect用法及代码示例
- Node.js Worker.disconnect()用法及代码示例
- Node.js Worker.send(message[, sendHandle[, options]][, callback])用法及代码示例
- Node.js Worker用法及代码示例
- Node.js Writable Stream pipe事件用法及代码示例
- Node.js WritableStream用法及代码示例
- Node.js Writable Stream finish事件用法及代码示例
- Node.js Writable Stream unpipe事件用法及代码示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代码示例
- Node.js http2.Http2ServerRequest request.url用法及代码示例
- Node.js request.socket用法及代码示例
- Node.js assert.notEqual(actual, expected[, message])用法及代码示例
- Node.js tlsSocket.authorized用法及代码示例
- Node.js zlib.deflateRaw()用法及代码示例
- Node.js http.IncomingMessage message.rawHeaders用法及代码示例
- Node.js Console用法及代码示例
- Node.js GM transparent()用法及代码示例
- Node.js URL.protocol用法及代码示例
- Node.js http.Agent.reuseSocket(socket, request)用法及代码示例
- Node.js fs.filehandle.datasync()用法及代码示例
- Node.js socket.bind()用法及代码示例
- Node.js v8.getHeapSpaceStatistics()用法及代码示例
- Node.js http2session.destroyed用法及代码示例
- Node.js http.ServerResponse response.statusCode用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 Worker.isDead()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。