Node.js + Python Child Process: Print returns data, but return doesn't

Node.js + Python子进程:打印数据返回,但还没有

问题 (Question)

I have a basic question. I'm writing beginning code with node.js + socket.io, in which I use a python child process and trigger actions when it returns data, viz:

io.sockets.on('connection',function(socket) {
    socket.on('load_api_call', function() {

        var pythonarray=['/Users/<omitting path>/test_return_sub_dict.py']

        var child=spawn('/Users/<omitting path>/python2.7',pythonarray)

        child.stdout.on('data',function(data){
            socket.emit('jsonSuccess',data.toString('utf8'));           
            console.log('python output: '+data);
        });

        child.stderr.on('data',function(data){
            process.stdout.write('error output: '+data);
        });

    });
});

This works fine. My question is this: when I print the data returned by in my python child process, the above "child.stdout.on('data'" will be activated by the returning data. However, when I just "return" the data, no data is returned, or anyway isn't returned in a way socket can hear. So for example in my child process module called "return sub dictionary", the last line

return sub_dict

will not return anything that socket will notice. But this last line:

print sub_dict

would be noticed by socket and trigger the subsequent desired actions.

What is the correct way to return data from a python child process such that node will notice it?

I can see that I'm not understanding some basic mechanism here of Python, Node, or both, but I don't know what to call it to google an answer.

Thanks for your advice!

我有一个基本的问题。我用Node.js +美国写作开始代码,我在使用Python的子过程和触发器动作的时候,它返回的数据,即:

io.sockets.on('connection',function(socket) {
    socket.on('load_api_call', function() {

        var pythonarray=['/Users/<omitting path>/test_return_sub_dict.py']

        var child=spawn('/Users/<omitting path>/python2.7',pythonarray)

        child.stdout.on('data',function(data){
            socket.emit('jsonSuccess',data.toString('utf8'));           
            console.log('python output: '+data);
        });

        child.stderr.on('data',function(data){
            process.stdout.write('error output: '+data);
        });

    });
});

这工作得很好。我的问题是这样的:当我打印数据返回我的Python子进程,以上的孩子”。在stdout。(数据”将被返回的数据激活。然而,当我的“回报”的数据,没有返回任何数据,或至少不在一路插座还能听到。例如在我的子进程模块称为“返回子词典》,最后一行

return sub_dict

将不会返回任何插座将通知。但这一行:

print sub_dict

会引起插座和触发所需的后续行动。

数据从一个Python的孩子的过程中,节点会注意到它返回正确的方法是什么?

我可以看到,我不了解一些基本的机制在Python,节点,或两个,但我不知道它叫什么给谷歌一个答案。

谢谢你的建议!

最佳答案 (Best Answer)

The python script has to output to stdout or stderr for node to pick data up because those are the streams that the child process object is watching. AFAIK just returning a value does not automatically display that data, so you have to tell python to manually print to stdout or stderr.

Python脚本输出到stdout或stderr节点选择数据流,因为这些孩子的过程对象看。据我所知,只返回一个值不会自动显示数据,所以你必须告诉python手动打印到stdout或stderr。

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/23451359