【发布时间】:2018-12-25 18:20:32
【问题描述】:
我需要从 NodeJS 服务代码中调用 python 函数。我检查了this link并在nodeJS代码下面写了
const express = require('express')
const app = express()
let runPy = new Promise(function(success, nosuccess) {
const { spawn } = require('child_process');
const pyprog = spawn('python', ['./ml.py']);
pyprog.stdout.on('data', function(data) {
success(data);
});
pyprog.stderr.on('data', (data) => {
nosuccess(data);
});
});
app.get('/', (req, res) => {
res.write('welcome\n');
runPy.then(function(testMLFunction) {
console.log(testMLFunction.toString());
res.end(testMLFunction);
});
})
app.listen(4000, () => console.log('Application listening on port 4000!'))
假设我在ml.py 中有一个示例 python 代码,如下所示
def testMLFunction():
return "hello from Python"
现在,当我运行 nodeJS 代码并通过 Curl 执行 GET 时,我只看到消息 'welcome' 这是 GET 端点的控制台日志。但是我在任何地方都看不到从 python 函数返回的消息。我错过了什么?
【问题讨论】: