我可以在Linux上打开一个命名管道,用于在Python中进行非阻塞写入吗?

我使用mkfifo创建了一个fifo文件.是否可以不受阻塞地打开/写入?我想知道是否有读者是不可知论者.

下列:

with open('fifo','wb',0) as file:
    file.write(b'howdy')

只是在露天停下来,直到我从另一个外壳做了一个cat fifo.我希望我的程序能够取得进步,无论数据消费者是否正在观看.

我应该使用不同的Linux机制吗?

解决方法

来自man 7 fifo:

A process can open a FIFO in nonblocking mode. In this case,opening or read-only will succeed even if no-one has opened on the write side yet,opening for write-only will fail with ENXIO (no such device or address) unless the other end has already been opened.

所以第一个解决方案是使用O_NONBLOCK打开FIFO.在这种情况下,您可以检查errno:如果它等于ENXIO,那么您可以稍后尝试打开FIFO.

import errno
import posix

try:
    posix.open('fifo',posix.O_WRONLY | posix.O_NONBLOCK)
except OSError as ex:
    if ex.errno == errno.ENXIO:
        pass # try later

另一种可能的方法是使用O_RDWR标志打开FIFO.在这种情况下它不会阻止.其他进程可以使用O_RDONLY打开它而没有问题.

import posix
posix.open('fifo',posix.O_RDWR)

相关文章

insmod和modprobe加-f参数导致Invalid module format错误 这...
将ArchLinux安装到U盘 几个月前入门Arch的时候上网搜了不少安...
1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...
如何抑制stable_secret读取关键的“net.ipv6.conf.all.stabl...