Diciamo che ho un parent.js
contenente un metodo chiamato parent
Come passare la funzione/callback al processo figlio in Node.js?
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();
});
}
Nel frattempo 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
});
});
});
mi chiedevo se le funzioni di passaggio al processo figlio non è consentito in Node.js.
Perché lo asyncFn
diventa undefined
dopo essere stato inviato allo child
?
E 'relativo a JSON.stringify
?
Aww, mi ha battuto ad esso: P – loganfsmyth
è 'new Function ('ritorno' + funcString)() ; 'sicuro per codice non affidabile? – M4GNV5
@ M4GNV5 No, dovresti invece utilizzare il modulo 'vm' in un processo figlio con le protezioni appropriate a livello di sistema operativo per essere sicuro con" codice non attendibile ". – mscdex