Node.js的cluster模块用于使用多核CPU

我们来看看cluster内部的机制:

EventEmitter {
  domain: null,
  _events: {},
  _eventsCount: 0,
  _maxListeners: undefined,
  Worker://有一个worker对象
   { [Function: Worker]
     super_:
      { [Function: EventEmitter]
        EventEmitter: [Circular],
        usingDomains: false,
        defaultMaxListeners: 10,
        init: [Function],
        listenerCount: [Function]
     }
  },
  isWorker: false,//isWorker函数
  isMaster: true,//isMaster函数
  workers: {},//workers属性
  settings: {},//settings对象
  schedulingPolicy: 1,//schedulingPolicy属性
  SCHED_NONE: 1,
  SCHED_RR: 2,
  setupMaster: [Function],//setupMaster方法
  fork: [Function],//fork函数
  disconnect: [Function] 
  //disconnect函数
}
我们再来看看cluster产生的worker内部是什么机制:

Worker {
  domain: null,
  _events: { message: [Function] },
  _eventsCount: 1,
  _maxListeners: undefined,
  suicide: undefined,//worker.suicide用于判断进程是自己退出还是意外退出的,然后master据此判断是否需要重新产生一个worker,调用kill/disconnect就是undefined
  state: 'none',
  id: 1,//进程唯一一个ID
  process://产生的worker有一个process属性,其值为一个ChildProcess实例
   ChildProcess {
     domain: null,
     _events:
      { internalMessage: [Object],
        error: [Function],
        message: [Function],
        exit: [Object],
        disconnect: [Object] },
     _eventsCount: 5,
     _maxListeners: undefined,
     _closesNeeded: 2,
     _closesGot: 0,
     connected: true,
     signalCode: null,
     exitCode: null,
     killed: false,
     spawnfile: 'C:\\Program Files\\nodejs\\node.exe',
     _handle: Process { owner: [Circular], onexit: [Function], pid: 3520 },
     spawnargs:
      [ 'C:\\Program Files\\nodejs\\node.exe',
        'C:\\Users\\Administrator\\Desktop\\N-blog\\fiboo\\cal' ],
     pid: 3520,
     stdin: null,
     stdout: null,
     stderr: null,
     stdio: [ null, null, null, null ],
     _channel:
      Pipe {
        _externalStream: {},
        fd: -1,
        writeQueueSize: 0,
        buffering: false,
        onread: [Function],
        sockets: [Object] },
     _handleQueue: null,
     send: [Function],
     _send: [Function],
     disconnect: [Function],
     _disconnect: [Function] 
 } }

你可能感兴趣的:(Node.js的cluster模块用于使用多核CPU)