0

假设我有一个像这样的异步操作:

    async () => {
       await longTask().then(result => console.log(result));
    }

假设 longTask 是不可预测的。它挂起并且有时无法解决。

我知道我们不能仅仅中止异步操作。

我查看了工作线程,以便我可以生成一个工作人员来运行此异步操作并在需要时终止它。

不幸的是,当我尝试它时,整个程序都退出了。

有没有办法让我中止/终止/取消/杀死工作线程并删除异步操作,而不退出节点应用程序?

谢谢你的时间。

4

1 回答 1

0

Borrowing from the Node.js documentation on Cluster here, a possible approach can be the following:

Main.js

const { fork } = require('child_process');
const WORKER_PATH = 'PATH_TO_WORKER_JS';
const INTERVAL = 10000; // 10 seconds
let CHECKS = 0; // keep count of how many times you check for completion

const worker = fork(WORKER_PATH);

// send message to start the async process

worker.send({ start: true }, err => {
   if(err) { // handle error }
});

// check for completion on the worker

setTimeout(() => {
    worker.send({ checkIsDone: true }, err => {});
}, INTERVAL);

// listen for message from worker

function checkMsg(msg) {
   const { done } = msg;
   if(done) {
       // async process ended; kill the worker
       worker.kill('SIGKILL');
   } else {
      if(check > 10) {
         // checked 10 times for completion, 10 seconds apart; 
         // process not terminating so force kill
         worker.send('SIGKILL');
      } else {
         // setup another check in 10 seconds
         setTimeout(() => {
             worker.send({ checkIsDone: true }, err => {});
         }, INTERVAL);
      }
      CHECKS++;
   }
}

process.on('message', checkMsg);

Worker.js

let isDone = false;

async function indeterministicProcess() {

   // some long computation
   
   // finished
}

process.on('message', msg => {
    const { checkIsDone, start } = msg;
    if(start) { 
        // start your async process 
        interdeterministicProcess().then(() => (isDone = true)).catch(e => console.error(e));
    } else if(checkIsDone) {
       if(isDone) {
           // send done message to master
           process.send({ done: true}, err => {});
       } else {
          process.send({ done: false}, err => {});
       }

    }
});

This is an approach I have used a few times in the past and has worked for me really well. Hope this gives you some idea on how to implement this for your use case.

于 2021-03-08T09:00:07.897 回答