通过child_进程将JSON从Python发送到节点会被截断,如果太长,如何修复?

通过child_进程将JSON从Python发送到节点会被截断,如果太长,如何修复?,python,node.js,stdout,child-process,Python,Node.js,Stdout,Child Process,我的Node&Python后端运行得很好,但我现在遇到了一个问题:如果我从Python发送回来的JSON没有节点太长,它会被一分为二,节点端的JSON.parse会失败 我该如何解决这个问题?例如,第一批剪辑在 ... [1137.6962355826706, -100.78015825640887], [773.3834338399517, -198 第二个有剩下的几个条目 .201506231888], [-87276.575065248, -60597.8827676457], [793

我的Node&Python后端运行得很好,但我现在遇到了一个问题:如果我从Python发送回来的JSON没有节点太长,它会被一分为二,节点端的JSON.parse会失败

我该如何解决这个问题?例如,第一批剪辑在

... [1137.6962355826706, -100.78015825640887], [773.3834338399517, -198
第二个有剩下的几个条目

.201506231888], [-87276.575065248, -60597.8827676457], [793.1850250453127, 
-192.1674702207991], [1139.4465453979683, -100.56741252031816], 
[780.498416769341, -196.04064849430705]]}
我是否必须为长JSON在节点端创建一些逻辑,或者这是我在Python端遇到的某种缓冲问题,我可以通过适当的设置来克服?以下是我在python方面所做的全部工作:

outPoints, _ = cv2.projectPoints(inPoints, np.asarray(rvec), 
np.asarray(tvec), np.asarray(camera_matrix), np.asarray(dist_coeffs))

# flatten the output to get rid of double brackets per result before JSONifying
flattened = [val for sublist in outPoints for val in sublist]
print(json.dumps({'testdata':np.asarray(flattened).tolist()}))
sys.stdout.flush()
在节点端:

// Handle python data from print() function
  pythonProcess.stdout.on('data', function (data){

    try {
      // If JSON handle the data
      console.log(JSON.parse(data.toString()));
    } catch (e) {
      // Otherwise treat as a log entry
      console.log(data.toString());
    }
  });

发出的数据是分块的,因此如果要解析
JSON
,则需要加入所有分块,并在
end
上执行
JSON.parse

默认情况下,将建立stdin、stdout和stderr的管道 在父Node.js进程和生成的子进程之间。这些管子 具有有限的(和特定于平台的)容量。如果子进程 写入到stdout超过该限制,而不输出 捕获时,子进程将阻塞,等待管道缓冲区恢复 接受更多数据

在每个块中,限制为
65536
字节

在2.6.11之前的Linux版本中,管道的容量是相同的 作为系统页面大小(例如,i386上的4096字节)。自Linux以来 2.6.11,管道容量为65536字节


非常感谢。清晰简洁的回答我应该如何向节点发送日志信息?现在我正在组合缓冲区,我不能使用print()发送任何日志消息,因为这些消息现在将与我发送的任何JSON数据连接在一起。是否有方法手动触发stdout.on('end')以清除缓冲区?
let result = '';
pythonProcess.stdout.on('data', data => {
    result += data.toString();
    // Or Buffer.concat if you prefer.
});

pythonProcess.stdout.on('end', () => {
    try {
      // If JSON handle the data
      console.log(JSON.parse(result));
    } catch (e) {
      // Otherwise treat as a log entry
      console.log(result);
    }
});