Skip to main content
 首页 » 编程设计

Python 从命名管道获取数据

2024年09月07日11dudu

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

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

请您参考如下方法:

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