python - 多个进程异步读取同一管道

标签 python linux

如何将数据从一个管道传送到三个不同的进程?

nulfp = open(os.devnull, "w")

piper = Popen([
    "come command",
    "some params"
], stdout = PIPE, stderr = nulfp.fileno())

pipe_consumer_1 = Popen([
    "come command",
    "some params"
], stdin = piper.stdout, stderr = nulfp.fileno())

pipe_consumer_2 = Popen([
    "come command",
    "some params"
], stdin = piper.stdout, stderr = nulfp.fileno())

pipe_consumer_3 = Popen([
    "come command",
    "some params"
], stdin = piper.stdout, stderr = nulfp.fileno())

pipe_consumer_1.communicate()
pipe_consumer_2.communicate()
pipe_consumer_3.communicate()
piper.communicate()

如果我运行上面的代码,它会产生一个损坏的文件。这意味着管道消费者可能不会读取管道的完整输出。

这个可以正常工作,但速度要慢得多:

nulfp = open(os.devnull, "w")

piper_1 = Popen([
    "come command",
    "some params"
], stdout = PIPE, stderr = nulfp.fileno())

piper_2 = Popen([
    "come command",
    "some params"
], stdout = PIPE, stderr = nulfp.fileno())

piper_3 = Popen([
    "come command",
    "some params"
], stdout = PIPE, stderr = nulfp.fileno())

pipe_consumer_1 = Popen([
    "come command",
    "some params"
], stdin = piper_1.stdout, stderr = nulfp.fileno())

pipe_consumer_2 = Popen([
    "come command",
    "some params"
], stdin = piper_2.stdout, stderr = nulfp.fileno())

pipe_consumer_3 = Popen([
    "come command",
    "some params"
], stdin = piper_3.stdout, stderr = nulfp.fileno())

pipe_consumer_1.communicate()
pipe_consumer_2.communicate()
pipe_consumer_3.communicate()
piper_1.communicate()
piper_2.communicate()
piper_3.communicate()

关于如何使第一个代码片段以与第二个代码片段相同的方式工作的任何建议?如果我使用第一种方法,该过程将在 1/3 的时间内完成。

最佳答案

这只使用一个字节的“ block ”,但你明白了。

from subprocess import Popen, PIPE

cat_proc = '/usr/bin/cat'

consumers = (Popen([cat_proc], stdin = PIPE, stdout = open('consumer1', 'w')),
             Popen([cat_proc], stdin = PIPE, stdout = open('consumer2', 'w')),
             Popen([cat_proc], stdin = PIPE, stdout = open('consumer3', 'w'))
)


with open('inputfile', 'r') as infile:
   for byte in infile:
       for consumer in consumers:
           consumer.stdin.write(byte)

测试时,消费者输出文件与输入文件匹配。

编辑: 这是从具有 1K block 的进程中读取的。

from subprocess import Popen, PIPE

cat_proc = '/usr/bin/cat'

consumers = (Popen([cat_proc], stdin = PIPE, stdout = open('consumer1', 'w')),
             Popen([cat_proc], stdin = PIPE, stdout = open('consumer2', 'w')),
             Popen([cat_proc], stdin = PIPE, stdout = open('consumer3', 'w'))
)

producer = Popen([cat_proc, 'inputfile'], stdout = PIPE)

while True:
    byte = producer.stdout.read(1024)
    if not byte: break
    for consumer in consumers:
        consumer.stdin.write(byte)

关于python - 多个进程异步读取同一管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11561201/

相关文章:

linux - 将主脚本变量传递到 Perl 模块

python - 我想在使用 python mechanize 进行身份验证后获取网站的源代码。 HTTP 错误 403 : Forbidden

c++ - Window 的 GetAsyncKeyState 的 Linux 等效项是什么?

linux - 如果程序在 shell 脚本中输出特殊字符串,则执行命令

python - 为什么我的 fastapi 或 uvicorn 会关闭?

python - 将嵌套循环计算转换为 Numpy 以加快速度

python - Pandas 两个数据框交叉连接

python - 无法以足够的精度将 matlab datenum 格式转换为 python datetime

Python wave : Extracting stereo channels separately from *. wav 文件

Linux shell脚本运行多个shell脚本选项卡