简体   繁体   English

在nodejs和Python之间交换数据

[英]Exchanging data between nodejs and Python

I am new to node and socket.io. 我是node和socket.io的新手。 I have a nodejs server that I am trying to use to both spawn a Python process from, and pass data to and from a web page. 我有一个nodejs服务器,我试图用它来生成Python进程,并从网页传递数据。 On the Python end I'm trying to use Python's stdin/stdout pipes. 在Python端,我正在尝试使用Python的stdin / stdout管道。

Everything coming from Python to nodejs and on to the server works fine. 从Python到nodejs到服务器的一切都运行良好。 However, when I attempt to pass data from nodejs back to Python using stdin.write, node crashes with a "write after end" error. 但是,当我尝试使用stdin.write将数据从nodejs传递回Python时,节点因“写入结束”错误而崩溃。 I am wondering if the socket I'm using to receive from Python is conflicting with that I'm trying to send back. 我想知道我用来接收Python的套接字是否与我想要发回的套接字冲突。 I am using a timer to send data to the web page every second. 我正在使用计时器每秒向网页发送数据。

This seems to have something to do with Python expecting an EOF when calling sys.stdin.readlines(), however, when I use stdin.end() from nodejs, that's when I get the error. 这似乎与Python在调用sys.stdin.readlines()时期望EOF有关,但是,当我使用来自nodejs的stdin.end()时,就是当我得到错误时。 Interestingly, if I never call stdin.end, I don't get the error, but I also don't receive anything. 有趣的是,如果我从不调用stdin.end,我没有收到错误,但我也没有收到任何错误。

Any help is greatly appreciated. 任何帮助是极大的赞赏。 I'm afraid I don't know enough about socket.io to see what's going wrong here. 我担心我对socket.io不够了解,看看这里出了什么问题。 Thank you. 谢谢。

IN NODEJS: Spawning the Python process: IN NODEJS:产生Python过程:

var util = require('util'),
spawn = require('child_process').spawn,
py = spawn('python', ['data_collect.py']);

Receiving data from Python: 从Python接收数据:

py.stdout.on('data', function(data){
    python_output = data.toString();

Sending data to Browser: 将数据发送到浏览器:

var listener = io.listen(server);
listener.sockets.on('connection', function (socket) {
    socket.emit( 'dataPacket1', {variable_1: data1});
    socket.emit( 'dataPacket2', {variable_2: data2});
    socket.emit( 'dataPacket3', {variable_3: data3});
}, 1000); 

Receiving from browser and sending to Python: 从浏览器接收并发送到Python:

socket.on('messageFromBrowser', function(data) {
    py.stdin.setEncoding('utf-8');
    py.stdin.write(JSON.stringify(data.browser_value+'\n'));
    py.stdin.end();   <--- Error happens here

}); });

IN PYTHON: Sending to nodejs: 在PYTHON:发送到nodejs:

output = "dataout"
sys.stdout.write(output)
sys.stdout.flush() 

Receiving from nodejs: 从nodejs接收:

lines = []

while True:
    line = sys.stdin.readline()
    if line !='':
        lines.append(line)
    else:
        break
return lines

if you want to combine node and python I think the best solution is to move to a microservice architecture using Seneca.JS. 如果你想组合node和python,我认为最好的解决方案是使用Seneca.JS转向微服务架构。 If I was in you I will create a rest interface to communicate between the a python process and node.js microservice. 如果我在你身边,我将创建一个休息接口,以便在python进程和node.js微服务之间进行通信。 you can find more information here 你可以在这里找到更多信息

Remove the py.stdin.end(). 删除py.stdin.end()。 And make sure '\\n' is outside stringify 并确保'\\ n'在stringify之外

py.stdin.write(JSON.stringify(data.browser_value)+'\n');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM