我正在使用next方法从node.js脚本中调用python脚本,
const spawn = require("child_process").spawn;
const pythonProcess = await spawn('python', ["script.py", "test",]);
如何使python脚本输出与node.js输出一起显示在控制台中?
答案 0 :(得分:1)
您可以稍微修改一下nodejs代码以获取输出
假设这是一个python脚本
#!/usr/bin/python
#
# for-loop.py: Sample for loop to print welcome message 3 times
#
for i in '123':
print "Welcome",i,"times"
Nodejs脚本
require("child_process").spawn('python', ['./loop.py'], {
cwd: process.cwd(),
detached: true,
stdio: "inherit"
});
或者您也可以检查python进程代码。
var childProcess = require("child_process").spawn('python', ['./loop.py'], {stdio: "inherit"})
childProcess.on('data', function(data){
process.stdout.write("python script output",data);
});
childProcess.on('close', function(code) {
if ( code === 1 ){
process.stderr.write("error occured",code);
process.exit(1);
}
else{
process.stdout.write('"python script exist with code: ' + code + '\n');
}
});