admin管理员组

文章数量:1414930

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.

本文标签: javascriptIs there a way to force terminate a worker thread in nodejsStack Overflow