Python 从命名管道获取数据

Python get data from named pipe

如何在 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

有办法解决这个问题吗?很抱歉这个问题。

os.mkfifo is used to create fifo. Use open 到 open/read 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