Node.js 如何在nodejs中监听来自工作者的终止信号?

Node.js 如何在nodejs中监听来自工作者的终止信号?,node.js,multithreading,node-worker-threads,Node.js,Multithreading,Node Worker Threads,我有一个主进程,它使用worker\u threads生成一个新线程。在某些特定情况下,主进程必须关闭此线程,而不管它是否完成了任务,因此主线程使用来停止线程。但是,此线程会产生不同的依赖项,需要在退出之前关闭这些依赖项。这些依赖项必须从线程关闭,因此我不能使用worker.on('exit'),因为它是在主进程上运行的 是否有某种方法可以从工人自己那里听到终止消息 一些我想要实现的最简单的例子 const {Worker, isMainThread} = require('worker_thr

我有一个主进程,它使用
worker\u threads
生成一个新线程。在某些特定情况下,主进程必须关闭此线程,而不管它是否完成了任务,因此主线程使用来停止线程。但是,此线程会产生不同的依赖项,需要在退出之前关闭这些依赖项。这些依赖项必须从线程关闭,因此我不能使用
worker.on('exit')
,因为它是在主进程上运行的

是否有某种方法可以从工人自己那里听到终止消息

一些我想要实现的最简单的例子

const {Worker, isMainThread} = require('worker_threads');

if (isMainThread) {
  const worker = new Worker(__filename);
  worker.on('message', console.log);
  worker.on('error', console.log);
  worker.on('exit', console.log);

  setTimeout(() => {
    console.log('Worker is gonna be terminated');
    worker.terminate();
  }, 5000);
} else {
  (async () => {
    console.log('I am the worker');
    // This thread will spawn its own dependencies, so I want to listen here the terminate signal from 
    // mainThread to close the dependencies of this worker
    // Sth like the following will be awesome
    // thread.on('exit', () => { /* close dependencies */ })

    // Simulate a task which takes a larger time than MainThread wants to wait
    await new Promise(resolve => {
      setTimeout(resolve, 10000);
    });
  })();
}

您可以通过
worker.postMessage(value)
/
parentPort.on(“message”,(value)=>{…})
向工作线程发出退出信号,然后在工作线程中使用
process.exit()
。当然要先清理干净

我建议使用对象作为
,这样您就可以将多个命令或日期从主线程传递到工作线程

const { Worker, isMainThread, parentPort } = require("worker_threads");

if (isMainThread) {
  const worker = new Worker(__filename);
  worker.on("message", console.log);
  worker.on("error", console.log);
  worker.on("exit", console.log);

  setTimeout(() => {
    console.log("Worker is gonna be terminated");
    // replace worker.terminate(); with something like
    worker.postMessage({ exit: true });
    // maybe add another setTimeout with worker.terminate() just in case?
  }, 5000);
} else {
  (async () => {
    // listen for message and do things according to passed value
    parentPort.on("message", (value) => {
      // worker threads do not have multiple listeners available for passing different event,
      // therefore add one onMessage listener, and pass an object with commands/data from main thread
      if (value.exit) {
        // clean up
        console.log("doing cleanup");
        process.exit(0);
      }
    });
    // add other logic for receiving messages from main thread

    console.log("I am the worker");
    // This thread will spawn its own dependencies, so I want to listen here the terminate signal from
    // mainThread to close the dependencies of this worker
    // Sth like the following will be awesome
    // thread.on('exit', () => { /* close dependencies */ })

    // Simulate a task which takes a larger time than MainThread wants to wait
    await new Promise((resolve) => {
      setTimeout(resolve, 10000);
    });
  })();
}

您可以通过
worker.postMessage(value)
/
parentPort.on(“message”,(value)=>{…})
向工作线程发出退出信号,然后在工作线程中使用
process.exit()
。当然要先清理干净

我建议使用对象作为
,这样您就可以将多个命令或日期从主线程传递到工作线程

const { Worker, isMainThread, parentPort } = require("worker_threads");

if (isMainThread) {
  const worker = new Worker(__filename);
  worker.on("message", console.log);
  worker.on("error", console.log);
  worker.on("exit", console.log);

  setTimeout(() => {
    console.log("Worker is gonna be terminated");
    // replace worker.terminate(); with something like
    worker.postMessage({ exit: true });
    // maybe add another setTimeout with worker.terminate() just in case?
  }, 5000);
} else {
  (async () => {
    // listen for message and do things according to passed value
    parentPort.on("message", (value) => {
      // worker threads do not have multiple listeners available for passing different event,
      // therefore add one onMessage listener, and pass an object with commands/data from main thread
      if (value.exit) {
        // clean up
        console.log("doing cleanup");
        process.exit(0);
      }
    });
    // add other logic for receiving messages from main thread

    console.log("I am the worker");
    // This thread will spawn its own dependencies, so I want to listen here the terminate signal from
    // mainThread to close the dependencies of this worker
    // Sth like the following will be awesome
    // thread.on('exit', () => { /* close dependencies */ })

    // Simulate a task which takes a larger time than MainThread wants to wait
    await new Promise((resolve) => {
      setTimeout(resolve, 10000);
    });
  })();
}

终止并执行函数worker.emit('exit')。然后,您可以在worker.on('exit',console.log)上监听它@是的,但我需要听工人自己说<代码>worker.on('exit',console.log)仅在父进程上可用。终止并执行函数worker.emit('exit')。然后,您可以在worker.on('exit',console.log)上监听它@是的,但我需要听工人自己说<代码>worker.on('exit',console.log)仅在父进程中可用。