python 管道 非阻塞_python – 什么条件导致打开的非阻塞命名管道(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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值