python not working correctly when called from nodejs app with child-process

与世无争的帅哥 提交于 2019-12-24 19:52:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!