2017-01-25 4 views
6

node-unfluff, HTML 문자열에서 내용을 추출하려고합니다. 그러나, 그것은 일반적으로 실행하는 데 ~ 200ms가 걸립니다. 동기식으로 실행되기 때문에 이것은 너무 느립니다. 비동기 적으로 실행되도록하고 싶습니다.Web Workers 대 Node_js의 CPU 집약적 기능을위한 child_process

내 알다시피, 내 옵션은 웹 근로자 (https://github.com/audreyt/node-webworker-threads) 또는 child_process (https://nodejs.org/api/child_process.html)입니다. 다른 더 나은 옵션이 있습니까?

그렇지 않은 경우 속도 나 기타 요인에서 어느 것이 더 낫습니까?

편집 :

도 있습니다 스레드 à 고골 락 (https://github.com/xk/node-threads-a-gogo)와 작은 작업자 (https://github.com/avoidwork/tiny-worker).

WebWorker Threads는 require을 지원하지 않으므로 더 이상 선택할 수 없습니다.

load 기능을 사용하여 require 개의 파일을 스레드 à gogo를 사용하여 파일 할 수 있지만 해킹 가능한 해결 방법처럼 보입니다.

tiny-worker는 현재 Github에서만 26 개의 별을 가지고 있으므로 프로덕션 코드에서 사용하는 것을 주저합니다. require을 지원합니다.

더 나은 옵션이 없다면 child_process을 사용하여 WebWorker 구현을 작성하려고합니다.

+0

당신이 좀 더 사용 사례를 설명 할 수 있습니까? 여러 개의 URL을 처리 할 때 병렬 처리를 시도하거나 노드 내의 성능을 어떻게 든 향상 시키려고합니까? Node의 라이브러리에 의존 할 때 하위 프로세스를 어떻게 사용 했습니까? 표준 노드 클러스터링이 목록에서 옵션이 아닌 이유는 무엇입니까 (실제로 자식 프로세스 자체에 의존한다는 것을 명심하십시오)? – SylonZero

+0

노드를 서버로 사용하고 있습니다. 함수를 실행하는 데 200ms가 걸리면 아무런 사용자도 200ms 이내에 서버 응답을 얻을 수 없습니다. 서버를 차단하지 않도록 비동기로 만들고 싶습니다. 'child_process'를 사용하여 다른 Node 프로세스를 시작할 수 있습니다. 전에는 노드 클러스터링을 사용 해본 적이 없지만, 각 코어에 대해 서버를 생성한다는 것을 이해했습니다. 'node-unfluff '가 모든 코어에서 실행 중이면 모든 서버가 차단됩니다. –

+0

자, 질문이 명확 해지기 시작했습니다. 그러나 함수의 인스턴스 (node-unfluff 사용)가 동기 적이더라도 Node에 대한 다른 요청의 다른 인스턴스가 실행되는 것을 막지는 않습니다. 따라서 한 명의 사용자가 200ms를 기다려야 할 수도 있지만 일반적으로 다른 * 사용자의 요청을 시작할 수는 없습니다.테스트를 통해이 unluff 모듈을 사용하면 실제로 동시 요청이 차단된다는 것을 알았습니까? – SylonZero

답변

1

근로자에게 필수 항목을 사용할 수 있습니다.

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. 
}); 

함께 퍼팅

작업자 : 문서를 필요에 따라 당신이 모듈에 구성 개체를 전달할 수있는 것처럼 당신의 노동자 스크립트에서는

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'}); 
    } 
}