从带有子进程的nodejs应用调用时python无法正常工作 [英] python not working correctly when called from nodejs app with child-process
问题描述
我正在尝试运行一个使用深度学习模型对图像进行分类的python脚本(无论是否为NSFW),我在这里使用此脚本 https://github.com/yahoo/open_nsfw 和一个自定义模型。直接用python调用时python脚本运行正常,但是通过子进程从nodejs应用程序调用时,脚本在此行停止
Im trying to run a python script that used deep learning model to classify an image wether it is NSFW or not, i use this script from here https://github.com/yahoo/open_nsfw and a custom model. The python script runs ok when called directly with python, but when called from nodejs app via child-process, the script stops at this line
# Pre-load caffe model.
nsfw_net = caffe.Net(model_def, pretrained_model, caffe.TEST)
其他所有内容python工作正常。而且脚本直接运行也很好。为什么从子进程调用时它不起作用?
Everything else in python works fine. And the script run directly works fine too. Why it is not working when called from child-process?
当我从这样的节点应用程序运行时
When i run it from a node app like this
const cp = require("child_process")
const spawn = cp.spawn;
const pythonProcess = spawn('python',['/home/ubuntu/classify.py']);
pythonProcess.stdin.write(req.body.image, function(err){
pythonProcess.stdin.end();
});
pythonProcess.stdout.on('data', (data) => {
console.log("message from python:" + data)
});
pythonProcess.stdout.on("end", (data) => {
console.log("end received data: " + data);
res.status(200);
res.end();
});
推荐答案
我已通过添加 pythonProcess.stderr.pipe(process.stderr);
到我的nodejs应用程序,然后我能够看到python错误并进行了修复。 python脚本中有错误。
I have fixed this by adding pythonProcess.stderr.pipe(process.stderr);
to my nodejs app and then i was able to see python errors and fixed them. There were mistakes in the python script.
这篇关于从带有子进程的nodejs应用调用时python无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!