使用pythonshell将TypedArray数据从NodeJS发送到Python的最佳方法

使用pythonshell将TypedArray数据从NodeJS发送到Python的最佳方法,python,node.js,Python,Node.js,我正在尝试使用Python shell将数据从NodeJS发送到Python中的进程。我的Python脚本需要一个float32数组,但我不确定如何使用Python shell发送该数据类型。我可以发送一个没有问题的字符串,而且我知道我的python脚本在其他方面工作正常。有没有直接发送数组的方法,或者我需要用python进行一些数据转换或解析 以下是我现在正在尝试的: 在Python中: import sys input = sys.stdin.read() print type(input)

我正在尝试使用Python shell将数据从NodeJS发送到Python中的进程。我的Python脚本需要一个float32数组,但我不确定如何使用Python shell发送该数据类型。我可以发送一个没有问题的字符串,而且我知道我的python脚本在其他方面工作正常。有没有直接发送数组的方法,或者我需要用python进行一些数据转换或解析

以下是我现在正在尝试的:

在Python中:

import sys
input = sys.stdin.read()
print type(input)
input_data = np.frombuffer(sys.stdin.read(), dtype=np.float32)
sys.stdout.write(input_data)
在节点中:

var PythonShell = require('python-shell');
var pyshell = new PythonShell('script.py', {mode:'binary'});
// data is float32 TypedArray
pyshell.send(data).end(function(err){
    if (err){console.log(err, 'did not work')};
});
pyshell.on('message', function (message) {
    console.log('message received', message); 
});
var options = {mode: 'binary'};
var pyshell = new PythonShell('test.py', options);
var data =  Buffer.from(myFloat32TypedArray.buffer, 'float32');     

pyshell.send(data).end((err) => {
    if (err){
        console.log(err);
    }else{
        console.log('data sent');
    };
});
    
pyshell.stdout.on('data', function (data) {
    console.log(data);
});
这里我得到了以下错误:

net.js:655
    throw new TypeError(
    ^

TypeError: Invalid data, chunk must be a string or buffer, not object
    at Socket.write (net.js:655:11)
    at PythonShell.send (/project/node_modules/python-shell/index.js:205:16)
    at Object.<anonymous> (/project/server.js:59:11)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
net.js:655
抛出新类型错误(
^
TypeError:无效数据,区块必须是字符串或缓冲区,而不是对象
at Socket.write(net.js:655:11)
在PythonShell.send(/project/node_modules/PythonShell/index.js:205:16)
在对象上。(/project/server.js:59:11)
在模块处编译(Module.js:570:32)
在Object.Module.\u extensions..js(Module.js:579:10)
在Module.load(Module.js:487:32)
在tryModuleLoad时(module.js:446:12)
在Function.Module.\u加载(Module.js:438:3)
位于Module.runMain(Module.js:604:10)
运行时(bootstrap_node.js:394:7)

如果我将TypedArray转换为字符串,它发送的结果很好,但是用Python而不是数组接收这个长字符串感觉不太好。我相信有一个简单的修复方法。任何建议都将不胜感激!

我不知道直接的方法(我想这也不可用)但是您可以在javascript端使用
JSON.stringify
将数组转换为字符串,将其发送到python,将其作为原始输入读取,并将其转换回JSON对象(即数组)

JAVASCRIPT端:

var a = [1,2,3];
pyshell.send(JSON.stringify(data), ....)
import json,sys
input = sys.stdin.read()
print(json.loads(input))
PYTHON端:

var a = [1,2,3];
pyshell.send(JSON.stringify(data), ....)
import json,sys
input = sys.stdin.read()
print(json.loads(input))

最后,我将float32数组转换为javascript缓冲区对象,并使用“二进制”模式。此外,我还需要从pyshell.on切换到pyshell.stdout.on,pyshell.stdout.on位于二进制模式的python shell测试脚本中,但不在自述文件中

在节点中:

var PythonShell = require('python-shell');
var pyshell = new PythonShell('script.py', {mode:'binary'});
// data is float32 TypedArray
pyshell.send(data).end(function(err){
    if (err){console.log(err, 'did not work')};
});
pyshell.on('message', function (message) {
    console.log('message received', message); 
});
var options = {mode: 'binary'};
var pyshell = new PythonShell('test.py', options);
var data =  Buffer.from(myFloat32TypedArray.buffer, 'float32');     

pyshell.send(data).end((err) => {
    if (err){
        console.log(err);
    }else{
        console.log('data sent');
    };
});
    
pyshell.stdout.on('data', function (data) {
    console.log(data);
});
在Python中:

import sys
input = sys.stdin.read()
print type(input)
input_data = np.frombuffer(sys.stdin.read(), dtype=np.float32)
sys.stdout.write(input_data)

也许这有帮助?消息建议您可以使用缓冲区对象,这应该是最好的选择。我同意这似乎是最好的选择。如果我发送JS缓冲区(例如buffer.from('test')),我不会收到上述错误,我会在pyshell.send()上命中成功回调调用,但是由于某种原因python脚本没有执行。我想知道它是否与使用{mode:'binary'}有关?我没有一个用于缓冲区的工作示例二进制模式(从yetGot it=)谢谢。我会在2天内接受我的回答,所以让我。。。