最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass functioncallback to child process in Node.js? - Stack Overflow

programmeradmin0浏览0评论

Let's say I have a parent.js containing a method named parent

var childProcess = require('child_process');

var options = {
    someData: {a:1, b:2, c:3},
    asyncFn: function (data, callback) { /*do other async stuff here*/ }
};

function Parent(options, callback) {
    var child = childProcess.fork('./child');
    child.send({
        method: method,
        options: options
    });
    child.on('message', function(data){
        callback(data,err, data,result);
        child.kill();
    });
}

Meanwhile in child.js

process.on('message', function(data){
    var method = data.method;
    var options = data.options;
    var someData = options.someData;
    var asyncFn = options.asyncFn; // asyncFn is undefined at here
    asyncFn(someData, function(err, result){
        process.send({
            err: err,
            result: result
        });
    });
});

I was wondering if passing functions to child process is not allowed in Node.js.

Why would asyncFn bee undefined after it is sent to the child?

Is it related to JSON.stringify?

Let's say I have a parent.js containing a method named parent

var childProcess = require('child_process');

var options = {
    someData: {a:1, b:2, c:3},
    asyncFn: function (data, callback) { /*do other async stuff here*/ }
};

function Parent(options, callback) {
    var child = childProcess.fork('./child');
    child.send({
        method: method,
        options: options
    });
    child.on('message', function(data){
        callback(data,err, data,result);
        child.kill();
    });
}

Meanwhile in child.js

process.on('message', function(data){
    var method = data.method;
    var options = data.options;
    var someData = options.someData;
    var asyncFn = options.asyncFn; // asyncFn is undefined at here
    asyncFn(someData, function(err, result){
        process.send({
            err: err,
            result: result
        });
    });
});

I was wondering if passing functions to child process is not allowed in Node.js.

Why would asyncFn bee undefined after it is sent to the child?

Is it related to JSON.stringify?

Share Improve this question asked Jan 20, 2015 at 4:11 kitkit 3253 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

JSON doesn't support serializing functions (at least out of the box). You could convert the function to its string representation first (via asyncFn.toString()) and then re-create the function again in the child process. The problem though is you lose scope and context with this process, so your function really has to be standalone.

Complete example:

parent.js:

var childProcess = require('child_process');

var options = {
  someData: {a:1, b:2, c:3},
  asyncFn: function (data, callback) { /*do other async stuff here*/ }
};
options.asyncFn = options.asyncFn.toString();

function Parent(options, callback) {
  var child = childProcess.fork('./child');
  child.send({
    method: method,
    options: options
  });
  child.on('message', function(data){
    callback(data,err, data,result);
    child.kill();
  });
}

child.js:

process.on('message', function(data){
  var method = data.method;
  var options = data.options;
  var someData = options.someData;
  var asyncFn = new Function('return ' + options.asyncFn)();
  asyncFn(someData, function(err, result){
    process.send({
      err: err,
      result: result
    });
  });
});
发布评论

评论列表(0)

  1. 暂无评论