简体   繁体   English

如何检测Python中的fifo已被删除

[英]How do I detect that a fifo has been deleted in Python

In Python, I can poll for incoming data on a fifo (created with the Linux mkfifo command) with: 在Python中,我可以使用以下命令在fifo(使用Linux mkfifo命令创建)上轮询传入数据:

reader = open(known_fifo_name,"r")
while True:
   data = reader.read(1)
   if data:
      process(data)
   else:
      time.sleep(0.1) #no data now, try later

But read() returns the empty string both when there is no data, and when the fifo has been deleted (by an external program with a remove() call). 但是read()在没有数据时以及在已删除fifo(由带有remove()调用的外部程序read()返回空字符串。
Is there a way to tell that the fifo I am listening to no longer exists? 有没有办法告诉我正在收听的fifo不再存在?

You can do the following: 您可以执行以下操作:

import os
try:
    os.stat(reader.fileno())
except OSError:
    # The FIFO has gone away
sleep(0.1)  # Wait for some more input

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM