當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js Worker 'exit'事件用法及代碼示例


事件:'exit'

添加於:v0.11.2

參數
  • code <number> 退出代碼,如果它正常退出。
  • signal <string> 導致進程被終止的信號的名稱(例如 'SIGHUP' )。

類似於 cluster.on('exit') 事件,但特定於該工作人員。

import cluster from 'node:cluster';

const worker = cluster.fork();
worker.on('exit', (code, signal) => {
  if (signal) {
    console.log(`worker was killed by signal: ${signal}`);
  } else if (code !== 0) {
    console.log(`worker exited with error code: ${code}`);
  } else {
    console.log('worker success!');
  }
});const cluster = require('node:cluster');

const worker = cluster.fork();
worker.on('exit', (code, signal) => {
  if (signal) {
    console.log(`worker was killed by signal: ${signal}`);
  } else if (code !== 0) {
    console.log(`worker exited with error code: ${code}`);
  } else {
    console.log('worker success!');
  }
});

相關用法


注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品  'exit'事件。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。