1

I have a node.js application that I've moved into JXcore multithreading and yet couldn't figure out how to reset a task. On the current implementation, the server creates a sub process and send the jobs one by one.

When a job takes more than X seconds, the main process kills the sub process and skips the running task and logs it. Any of the jobs shouldn't take more than X seconds.

So far, I already moved the queue system into JXcore easily and it works as expected but I couldn't figure out yet, how can I kill the running task.

4

1 回答 1

2

Looks like the ability to kill the running task is a needed feature, since someone already asked the same question and it has been answered here: Thread lifetime management.

Upcoming JXcore release will have jxcore.tasks.killThread(). The logic is this: a task would inform the main thread, that it just has been started, and then the main thread may start counting the timeout for killing the thread, for example:

// main thread receives the message from a task
jxcore.tasks.on("message", function(threadId, obj){
    if(obj.started){
        //kill the task after a second
        setTimeout(function(){
            jxcore.tasks.killThread(threadId);
            console.log("thread killed", threadId);
        },1000);
    }
});

// adding a task
jxcore.tasks.addTask( function() {
    // informing the main thread, that task is just started
    process.sendToMain({started:true});

    // looping forever
    while(true){};
    console.log("this line will never happen.");
}); 
于 2014-03-25T09:18:37.970 回答