简体   繁体   中英

How to capture response from child process in nodejs

I am trying to capture some response from my child process in master process. I can log information of child process in master process but unable to capture some return xyz response . Here is code for master process:

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

Like in console i see output as : stdout====: Waiting for child Process (block.js) to complete...... -punws-sohan

stdout: ==== Thank You for waiting......

I cannot see output for return response statement Can anyone suggest how to capture response from child process?

First of all, the busy loop uses up unnecessary CPU time. Just use a setTimeout() instead. Example:

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

Secondly, you can't expect return to magically write the returned value to the parent process. Try this instead:

// 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM