2017-01-25 11 views
6

node-unfluffを使用しようとしていますが、これはHTML文字列からコンテンツを抽出しています。しかし、通常は実行するまでに約200msかかります。同期的に実行されるため、これは遅すぎます。私はそれを非同期に実行したい。Node.jsのCPU集約関数のWebワーカー対child_process

私の知る限り、私のオプションはWebワーカー(https://github.com/audreyt/node-webworker-threads)またはchild_process(​​)です。他にも良い選択肢がありますか?

そうでない場合は、スピードなどの点でどちらが優れていますか?

編集:

もありますスレッドàゴーゴー(https://github.com/xk/node-threads-a-gogo)と小さなワーカー(https://github.com/avoidwork/tiny-worker)。

WebWorkerスレッドはrequireをサポートしていないため、これ以上オプションはありません。

load機能を使用してスレッドàgogoを使用しているファイルはrequireになる可能性がありますが、これはハックのような回避策のようです。

tiny-workerは現時点でGithubには26個の星しか持っていないので、私は生産コードで使用することを躊躇しています。それはrequireをサポートしています。

もっと良いオプションがない場合は、child_processを使用して自分のWebWorker実装を作成することを検討しています。

+0

あなたはもう少しあなたのユースケースを説明できますか?複数のURLを処理する際に並列処理をしようとしているのですか、あるいは何らかの形でノード内のパフォーマンスを向上させようとしていますか? Nodeのライブラリに依存している場合、どのように子プロセスを使用することを構想しましたか?なぜ標準ノードクラスタリングはあなたのリストのオプションではありませんか(実際には子プロセス自体に依存していることに留意してください) – SylonZero

+0

私はノードをサーバーとして使用しています。関数の実行に200msかかる場合、ユーザーは200ms以内にサーバー応答を得ることができません。サーバーをブロックしないように非同期にする必要があります。 'child_process'を使って別のNodeプロセスを開始することができます。以前はノードクラスタリングを使ったことはありませんでしたが、各コア用のサーバーが作成されています。すべてのコアで 'node-unfluff 'が実行されていると、すべてのサーバーがブロックされます。 –

+0

あなたの質問はより明確になり始めています。ただし、(node-unfluffを使用して)関数のインスタンスが同期していても、Nodeへの他のリクエストの他のインスタンスが起動されることを防ぎません。したがって、1人のユーザーが200ミリ秒待つ必要があるかもしれませんが、通常は他のユーザーの要求を開始できないことを意味しません。あなたはテストして、このunluffモジュールを使用すると実際に同時要求をブロックすることが判明しましたか? – SylonZero

答えて

1

ワーカーにはrequireを使用できます。

requirejs.config({ 
    //By default load any module IDs from js/lib 
    baseUrl: 'js/lib', 
    //except, if the module ID starts with "app", 
    //load it from the js/app directory. paths 
    //config is relative to the baseUrl, and 
    //never includes a ".js" extension since 
    //the paths config could be for a directory. 
    paths: { 
     app: '../app' 
    } 
}); 

// Start the main app logic. 
requirejs(['jquery', 'canvas', 'app/sub'], 
function ($,  canvas, sub) { 
    //jQuery, canvas and the app/sub module are all 
    //loaded and can be used here now. 
}); 

一緒にそれを置く

ワーカーを:ドキュメントを要求ごとに、あなたがモジュールにconfigオブジェクトを渡すことができ、あなたの労働者のスクリプトでは、

self.importScripts('../path/require.js'); 

を呼び出す必要があります。 js

self.importScripts('../path/require.js'); 
requirejs.config({ 
    //By default load any module IDs from path/lib 
    baseUrl: 'path/lib', 
    //except, if the module ID starts with "app", 
    //load it from the js/app directory. paths 
    //config is relative to the baseUrl, and 
    //never includes a ".js" extension since 
    //the paths config could be for a directory. 
    paths: { 
     app: '../app' 
    } 
}); 

// Start the main app logic. 
requirejs(['jquery', 'canvas', 'app/sub'], 
function ($,  canvas, sub) { 
    //jQuery, canvas and the app/sub module are all 
    //loaded and can be used here now. 
    // now you can post a message back to your callee script to let it know require has loaded 
    self.postMessage("initialized"); 
}); 

self.onmessage = function(message) { 
    // do cpu intensive work here, this example is not cpu intensive... 
    if(message.data === 'to process') { 
     self.postMessage("completed!"); 
    } 
} 

ノードワーカーコール

var worker = new Worker('Worker.js'); 
worker.onmessage = function(event) { 
    var msg = event.data; 
    if(msg === 'initialized') { 
     worker.postMessage({data: 'to process'}); 
    } 
} 
関連する問題