最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to force terminate a worker thread in node.js? - Stack Overflow

programmeradmin0浏览0评论

I was looking around for my question but to no avail. I wanted to know if there was a way to force a worker to terminate a worker thread even if its task hadn't been pleted. I have some code in there that calls itself if it somehow fails, which is intentional even though it might be bad code practice. I tried using libraries such as microjob and threads, but those didn't have what I wanted. Here is what I tried to do with threads but didn't work:

// Module part of threads (taskModule)
expose(async function startTask(taskData, profileData, taskID) {

    await _Task.FetchData(taskData);
    await _Task.GenerateSession(profileData);
    await _Task.RequestPermission(taskID);
    await _Task.SubmitOrder();

    return "finished";
})
// Runs the module (main.js)
const {spawn, Thread, Worker} = require("threads");

let task = null;

async function test() {
    task = await spawn(new Worker('./taskModule'))
    startTask({},{},"0")
}

setTimeout(async function() {
    await Thread.terminate(task)
}, 3000)

test().then(() => {
    console.log('hello')
}).catch(console.error)

I was trying to test to see if the code would stop after 3 seconds to test, but it seems like it just continues. Is there a better way to do this type of task?

Thanks for any help.

发布评论

评论列表(0)

  1. 暂无评论