gpt4 book ai didi

python - 什么条件导致打开的非阻塞命名管道 (fifo) 为 "unavailable"用于读取?

转载 作者:IT王子 更新时间:2023-10-29 00:27:39 30 4
gpt4 key购买 nike

情况:

new_pipe = os.open(pipe_path, os.O_RDONLY | os.O_NONBLOCK) # pipe_path points to a FIFO
data = os.read(new_pipe, 1024)

读取偶尔会引发 errno -11:资源暂时不可用。

这个错误是什么时候出现的?这似乎很少见,因为常见的情况返回数据:

  • 如果没有 writer 打开管道,则返回空 str ('')。
  • 如果 writer 打开了管道,但 fifo 中没有数据,则为空 str('') 也被返回
  • 当然,如果写入者将数据放入 fifo,则该数据将被读取。

最佳答案

来自POSIX specification of the read system call (强调我的):

When attempting to read from an empty pipe or FIFO:

  • If no process has the pipe open for writing, read() shall return 0 to indicate end-of-file.

  • If some process has the pipe open for writing and O_NONBLOCK is set, read() shall return -1 and set errno to [EAGAIN].

所以基本上你的第二个假设是错误的:

If the writer has the pipe opened, but no data is in the fifo, empty str ('') is also returned

这将违反规范,我无法在我的机器上重现该行为(它为我引发了 EAGAIN)。不过这不是什么大问题,您可以捕获异常并重试:

import errno

def safe_read(fd, size=1024):
''' reads data from a pipe and returns `None` on EAGAIN '''
try:
return os.read(fd, size)
except OSError, exc:
if exc.errno == errno.EAGAIN:
return None
raise

关于python - 什么条件导致打开的非阻塞命名管道 (fifo) 为 "unavailable"用于读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10021759/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com