当我从函数调用它时,nodejs child_process生成不起作用

时间:2018-09-03 17:08:58

标签: node.js socket.io child-process spawn

我正在尝试与节点js中的python文件通信。当我编写下面的代码时,它将运行。

var _data = {
phoneNumber:254727677068,
amount:10
}

var spawn = require('child_process').spawn;
var scriptExecution = spawn("python", ["./nifty.py"]); 
   scriptExecution.stdout.on('data', (data) => {
   console.log("python output",String.fromCharCode.apply(null, data));
});

var data = JSON.stringify([_data.phoneNumber,parseInt(_data.amount),'kbsacco','f861c6cc-4efa-4306-8eee-08f035b03772']);

scriptExecution.stdin.write(data);

scriptExecution.stdin.end();  

但是当我在函数中使用它并调用它时,我什么也没回来...

makePayment = function(_data){
    // console.log("incoming: ",_data)
    var spawn = require('child_process').spawn;

    var scriptExecution = spawn("python", ["./nifty.py"]); 

   // Handle normal output
   scriptExecution.stdout.on('data', (data) => {
       console.log("python output",String.fromCharCode.apply(null, data));
   });

   var data = JSON.stringify([_data.phoneNumber,parseInt(_data.amount),'kbsacco','f861c6cc-4efa-4306-8eee-08f035b03772']);
   console.log("data: ",data)
   // Write data (remember to send only strings or numbers, otherwhise python wont understand)
   scriptExecution.stdin.write(data);
   // End data write
   scriptExecution.stdin.end();        
}

    socket.on('payLoan',(_data)=>{
        console.log("Here comes loan repayment: ",_data)
        // payments.makePayment(data.phone_number,data.amount) 

        var data = {
            phoneNumber:_data.phoneNumber,
            amount:_data.amount
        }
        makePayment(data)
    })

在socketio内部调用它不会产生任何结果

0 个答案:

没有答案