Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass function/callback to child process in Node.js?

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 become undefined after it is sent to the child?

Is it related to JSON.stringify?

like image 298
kit Avatar asked Jan 20 '15 04:01

kit


People also ask

How do I call a callback function in node JS?

For example, a function to read a file may start reading file and return the control to the execution environment immediately so that the next instruction can be executed. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as a parameter.

What is child_process spawn?

The node:child_process module provides the ability to spawn subprocesses in a manner that is similar, but not identical, to popen(3) . This capability is primarily provided by the child_process. spawn() function: const { spawn } = require('node:child_process'); const ls = spawn('ls', ['-lh', '/usr']); ls. stdout.


1 Answers

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
    });
  });
});
like image 62
mscdex Avatar answered Sep 28 '22 16:09

mscdex