2014-09-01 2 views
0

마스터 프로세스에서 내 자식 프로세스의 응답을 캡처하려고합니다. 마스터 프로세스에서 하위 프로세스의 정보를 기록 할 수 있지만 일부 반환 xyz 응답을 캡처 할 수 없습니다. 표준 출력 ==== : 자식 프로세스 (block.js)에 대한 대기가 ...... -punws를 완료하기 위해 콘솔에서노드 프로세스에서 응답을 캡처하는 방법

var express = require('express'); 
var app = express(); 
const 
      fs = require('fs'), 
      cp = require('child_process'); 
app.get('/',onRequest); 
    function onRequest(request, response) { 
       var express = require('express'); 
      var app = express(); 

      var child= cp.spawn('node' ,['./child_process/block.js'],filestreamCallback); 
      child.stdout.on('data', function(data) { 
       console.log('stdout: ==== ' + data); 
      }); 
      child.stderr.on('data', function(data) { 
       console.log('stdout: ' + data); 
      }); 
      child.on('close', function(code) { 
       console.log('closing code: ' + code); 
      }); 
     function filestreamCallback() { 
       response.writeHead(200, {'Content-Type': 'text/plain'}); 
       baflog.info("Reading Stream completed"); 
       response.write('Thanks for Your patience!\n'); 
       response.end(); 
      } 
     } 
     app.listen(5000); 
     console.log('Server started'); 

Child process : block.js 

     /*Keep waiting for 10 seconds*/ 

    console.log("Waiting for child Process (block.js) to complete......"); 
    var startTime = new Date().getTime(); 
    while (new Date().getTime() < startTime + 10000); 
    ret_resp(); 
    var response = {status:'success'}; 
    function ret_resp(){ 
     return response; 
    } 
    console.log("Thank You for waiting......"); 

처럼 내가 출력으로 참조 : 여기 마스터 프로세스 코드 -sohan

표준 출력 : ====이 ...... 대기 주셔서 감사합니다

나는 누군가가 자식 프로세스에서 응답을 캡처하는 방법을 제안 할 수 반환 응답 문 의 출력을 볼 수없는 이유는 무엇입니까?

답변

2

우선 busy 루프는 불필요한 CPU 시간을 소모합니다. 대신 setTimeout()을 사용하십시오. 예 :

setTimeout(function() { 
    ret_resp(); 
    // ... 
}, 10000); 

둘째, 당신은 return 마술 부모 프로세스로 반환 된 값을 기록 할 것으로 예상 할 수 없다. 대신 다음을 시도하십시오 :

// parent.js 
var child = cp.fork('./child_process/block.js', [], { silent: true }); 
child.stdout.on('data', function(data) { 
    console.log('stdout: ==== ' + data); 
}); 
child.stderr.on('data', function(data) { 
    console.log('stdout: ' + data); 
}); 
child.on('message', function(msg) { 
    console.log('message from child: ' + require('util').inspect(msg)); 
}); 
child.on('close', function(code) { 
    console.log('closing code: ' + code); 
}); 


// child.js 
console.log('Waiting for child Process (block.js) to complete......'); 
setTimeout(function() { 
    var response = {status:'success'}; 
    function ret_resp() { 
    process.send(response); 
    } 
    ret_resp(); 
    console.log('Thank You for waiting......'); 
}, 10000); 
+0

로그에 메시지를 배치하고 있습니다. 그럴 필요가 없습니다. 나는 우리가 포크처럼하는 것을 좋아한다. process.send (msg) 그러면 마스터 프로세스가 메시지를 수신합니다. 포크와 함께 가능하지만 나는 spawn/exec와 함께 길을 볼 수 없다. – Sohan

+0

'fork()'사용법을 포함하도록 답변을 업데이트했습니다. – mscdex

+0

오류가 발생했습니다. child_process.js : 402 throw 새 TypeError ('message undefined be undefined'); ^ 오류 : (: 402 : 13 child_process.js) 메시지 process.target.send 에 정의되지 않을 수 ret_resp에서 (d : \ 노드 JS \ baflog \ child_process \ block.js : 17 : 16)에서 널 Timer.listOnTimeout [ontimeout] (타이머 : 112 : 15) 종료 코드 : 8 – Sohan

관련 문제