1

JXcore マルチスレッドに移行した node.js アプリケーションがありますが、タスクをリセットする方法がわかりません。現在の実装では、サーバーはサブプロセスを作成し、ジョブを 1 つずつ送信します。

ジョブに X 秒以上かかると、メイン プロセスはサブ プロセスを強制終了し、実行中のタスクをスキップしてログに記録します。どのジョブも X 秒以上かかるべきではありません。

これまでのところ、キュー システムを JXcore に簡単に移行して期待どおりに動作しましたが、実行中のタスクを強制終了するにはどうすればよいか、まだわかりませんでした。

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 に答える