ワーカーには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'});
}
}
あなたはもう少しあなたのユースケースを説明できますか?複数のURLを処理する際に並列処理をしようとしているのですか、あるいは何らかの形でノード内のパフォーマンスを向上させようとしていますか? Nodeのライブラリに依存している場合、どのように子プロセスを使用することを構想しましたか?なぜ標準ノードクラスタリングはあなたのリストのオプションではありませんか(実際には子プロセス自体に依存していることに留意してください) – SylonZero
私はノードをサーバーとして使用しています。関数の実行に200msかかる場合、ユーザーは200ms以内にサーバー応答を得ることができません。サーバーをブロックしないように非同期にする必要があります。 'child_process'を使って別のNodeプロセスを開始することができます。以前はノードクラスタリングを使ったことはありませんでしたが、各コア用のサーバーが作成されています。すべてのコアで 'node-unfluff 'が実行されていると、すべてのサーバーがブロックされます。 –
あなたの質問はより明確になり始めています。ただし、(node-unfluffを使用して)関数のインスタンスが同期していても、Nodeへの他のリクエストの他のインスタンスが起動されることを防ぎません。したがって、1人のユーザーが200ミリ秒待つ必要があるかもしれませんが、通常は他のユーザーの要求を開始できないことを意味しません。あなたはテストして、このunluffモジュールを使用すると実際に同時要求をブロックすることが判明しましたか? – SylonZero