从Node.js代码中提取Python代码输出

时间:2019-04-04 04:55:17

标签: python node.js

我正在尝试获取javascript脚本的输出作为javascript代码中的变量(由node.js驱动)

python代码从javascript代码中获取值,而javascript代码已使用python-shell完成。我已经成功地将消息从python发送到javascript,而消息是python的字典。我尝试了python-shell接收函数,但是没有用,

var {PythonShell} = require('python-shell');

var pyshell = new PythonShell('CostFlow.py');

var data = {
start_nodes : [ 0, 0,  1, 1,  1,  2, 2,  3, 4],
end_nodes   : [ 1, 2,  2, 3,  4,  3, 4,  4, 2],
capacities  : [15, 8, 20, 4, 10, 15, 4, 20, 5],
unit_costs  : [ 4, 4,  2, 2,  6,  1, 3,  2, 3]
};

 pyshell.send(JSON.stringify(data));
 pyshell.on('message', function (message) {
     console.log(message);
 });

 // end the input stream and allow the process to exit
 pyshell.end(function (err) {
 if (err){
    throw err;
  };

console.log('finished');
 });

pyshell.on函数接受回调,该回调将我的python代码的输出打印为{'cost':150},我的意思是我想提取此值并在我的javascript代码中进一步使用它,这可能吗?

0 个答案:

没有答案