Python从命名管道获取数据

时间:2018-01-17 04:33:35

标签: python python-3.x pipe fifo

如何从Python 3.5.3中的命名管道读取?

管道的名称和位置为/tmp/shairport-sync-metadata,任何人都可以查看,但只能由shairport-sync用户修改。 其他网站和问题说要使用os.mkfifo("pipe-location"),但我一直收到此错误:

>>> import os
>>> os.mkfifo("/tmp/shairport-sync-metadata")

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    os.mkfifo("/tmp/shairport-sync-metadata")
FileExistsError: [Errno 17] File exists

有没有办法解决这个问题?抱歉,这是一个不起眼的问题。

1 个答案:

答案 0 :(得分:2)

os.mkfifo用于创建 fifo。使用open打开/读取fifo已存在:

with open('/tmp/shairport-sync-metadata') as f:   # add `rb` for binary mode
    # line-by-line read
    for line in f:
        print(line)

    # f.read(1024)  # to read 1024 characters