【问题标题】:Reduced named pipe (fifo) throughput with nodejs consumer使用 nodejs 消费者降低命名管道 (fifo) 吞吐量
【发布时间】:2018-10-17 19:11:58
【问题描述】:

我正在尝试使用命名管道将图像流从 python 进程传递到 nodejs 进程,理想情况下,对于 1 兆字节的图像,每秒 60 帧。我得到的吞吐量只有每秒 25 帧左右。我很惊讶它这么慢,所以我测试了将帧从一个 python 进程传输到第二个 python 进程。我能够达到每秒约 500 帧的传输速率。我是 nodejs 的新手,所以我很容易遗漏一些东西,但我认为它会有相当的速度。为什么我的 nodejs 程序在使用命名管道中的数据时这么慢?

我正在使用 nodejs 可读流并读取“可读”事件:

reader.js

const fs = require('fs');

fifo = fs.createReadStream(process.argv[2], { highWaterMark: 4*1024**2, });

let t0 = Date.now();
fifo.on('readable', () => {
  const chunk = fifo.read(1024 ** 2);
  if (chunk !== null) {
    let t1 = Date.now();
    process.stdout.write(`${(t1 - t0)/1000.}s, ${1000./(t1 - t0)}fps\n`);
    t0 = t1;
  }

});

fifo.on('end', () => {
  process.stdout.write('end');
});

我的 python 生产者只是将字节写入命名管道,就好像它是一个文件一样:

生产者.py

import sys
import numpy as np

im = np.random.randint(0, 255, size=(1024, 1024)).astype(np.uint8).ravel()

with open(sys.argv[1], 'wb') as f:
    while True:
        f.write(im.tobytes())
        f.flush()

python 阅读器只是从命名管道中读取,就好像它是一个文件一样:

reader.py

import sys
import numpy as np
import time

l = 1024 ** 2
t0 = time.time()
with open(sys.argv[1], 'rb') as f:
    while True:
        im = f.read(l)
        t1 = time.time()
        print('{}s, {}fps'.format(t1 - t0, 1/(t1 - t0)))
        t0 = t1

测试python到javascript的传输:

mkfifo /tmp/video; python producer.py /tmp/video & node reader.js /tmp/video

并测试 python 到 python 的传输:

mkfifo /tmp/video; python producer.py /tmp/video & python reader.py /tmp/video

我使用的是 mac(操作系统 10.13.6,2.7 GHz Intel Core i5)。 Python 3.7.0。节点 v8.9.1。

我也尝试使用 nodejs 阅读器的“数据”事件,它就像 慢的。 nodejs 事件是否有足够的开销来减慢读取速度?

任何想法将不胜感激!

【问题讨论】:

    标签: python node.js pipe


    【解决方案1】:

    这似乎是 macOS 的限制。

    据我所知,macOS 上的命名管道具有无法更改的固定缓冲区大小(大约为 16 或 64K),而在 Linux 上,大小可以调整到 1M。

    我测试过,reader.js 在 Linux 上的性能要好得多,与reader.py 差不多。

    一项快速测试表明,将 Python 进程作为子进程运行可以显着提高读取速度:

    // reader.js
    const { spawn } = require('child_process');
    const fifo = spawn('python', ['producer.py']).stdout; // (instead of fs.createReadStream(...))
    ...
    
    // producer.py
    ...
    f = sys.stdout.buffer
    while True:
        f.write(im.tobytes())
        f.flush()
    

    【讨论】:

      猜你喜欢
      • 2020-10-13
      • 2012-07-05
      • 2015-04-09
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多