问题
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)
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();
});
回答1:
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.
来源:https://stackoverflow.com/questions/56527430/python-not-working-correctly-when-called-from-nodejs-app-with-child-process