在NodeJS和Python之间进行通信:传回多个参数 [英] Communicating between NodeJS and Python: Passing back multiple arguments

查看:67
本文介绍了在NodeJS和Python之间进行通信:传回多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

截至目前,我正在使用内置的child_process启动Python脚本,并侦听通过stdout.on('data',(data))返回的任何数据,就像第一个JS代码的第6行一样.但是从Google搜索的结果来看,我只看到一个回传或一组回传的示例全部聚集在一起的例子.我想知道是否可能传回不止一个论点.下面是我的代码:

As of right now I am using the built in child_process to start up the Python script and listen for any data passed back via stdout.on('data', (data)) like in line 6 of the first JS code. But from the Google searches I have done I only see examples of one thing being passed back or a group of things being passed back all clumped together. I was wondering if it was possible to send back more than just one argument. Below is my code:

JS:

const spawn = require('child_process').spawn;
pythonProcess = spawn('python', ["/path/to/python/file"]);

pythonProcess.stdout.on('data', (data) => {
    console.log(data);
});

Python:

import sys

var thing1 = "Cold";
var thing2 = "Hot";
var thing3 = "Warm";

print(thing1);
print(thing2);
print(thing3);
sys.stdout.flush();

但是我想发生的事情可能是传回类似数组的内容,其中充满了我想发回的东西,这样我就可以像这样在JS文件中访问它们:

But what I want to happen is maybe pass back something like an array which is filled with the things I want to send back so that I can access them in the JS file like so:

const spawn = require('child_process').spawn;
pythonProcess = spawn('python', ["/path/to/python/file"]);

pythonProcess.stdout.on('data', (data) => {
    thing1 = data[0];
    thing2 = data[1];
    thing3 = data[2];
})

console.log('thing1: ' + thing1);
console.log('thing2: ' + thing2);
console.log('thing3: ' + thing3);

哪个会输出:

thing1: Hot
thing2: Cold
thing3: Warm

我该怎么做?

提前谢谢!

推荐答案

Node.js和Python之间没有直接通信的接口,因此您不能传递自定义参数,而您正在执行的只是执行使用 child_process 的python程序,因此您不会发送自变量,即在'data'上接收到的任何内容,其内容将从以下位置打印到 stdout python.

There isn't an interface that communicate directly between Node.js and Python, so you can't pass custom arguments, what you're doing is just executing a python program using child_process, so you don't send arguments, anything that is received on 'data' its what is printed to stdout from python.

因此,您需要做的是将数据序列化,然后在Node中反序列化,为此您可以使用 JSON .

So what you need to do, is serialize the data, and then deserialize it in Node, you can use JSON for this.

从您的python脚本中,输出以下 JSON 对象:

From your python script, output the following JSON object:

{
   "thing1": "Hot",
   "thing2": "Cold",
   "thing3": "Warm"
}

在您的Node.js脚本中:

And in your Node.js script:

const spawn = require('child_process').spawn;
const pythonProcess = spawn('python', ["/path/to/python/file"]);

const chunks = [];

pythonProcess.stdout.on('data', chunk => chunks.push(chunk));

pythonProcess.stdout.on('end', () => {

    try {
        // If JSON handle the data
        const data = JSON.parse(Buffer.concat(chunks).toString());

        console.log(data);
        // {
        //    "thing1": "Hot",
        //    "thing2": "Cold",
        //    "thing3": "Warm"
        // }

    } catch (e) {
        // Handle the error
        console.log(result);
    }
});

请记住,数据是分块的,因此在解析 JSON 之前必须等到发出 end 事件之前,否则 SyntaxError 将被触发.(

Have in mind that data is chunked, so will have to wait until the end event is emitted before parsing the JSON, otherwise a SyntaxError will be triggered. (Sending JSON from Python to Node via child_process gets truncated if too long, how to fix?)

您可以使用自己喜欢的任何类型的序列化,由于我们使用的是JavaScript,因此 JSON 是最简单的.

You can use any type of serialization you feel comfortable with, JSON is the easiest since we're in javascript.

请注意, stdout 是一个流,因此它是异步的,这就是您的示例永远无法工作的原因.

Note that stdout is a stream, so it's asyncrhonous, that's why your example would never work.

pythonProcess.stdout.on('data', (data) => {
    thing1 = data[0];
    thing2 = data[1];
    thing3 = data[2];
})

// Things do not exist here yet
console.log('thing1: ' + thing1);
console.log('thing2: ' + thing2);
console.log('thing3: ' + thing3);

这篇关于在NodeJS和Python之间进行通信:传回多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆