为什么只读打开一个命名管道块? [英] Why does a read-only open of a named pipe block?

查看:149
本文介绍了为什么只读打开一个命名管道块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Python处理各种UNIX(Linux,FreeBSD和MacOS X)命名管道(FIFO)时,我注意到了一些奇怪的地方。第一个,也许是最烦人的是试图打开一个空/空闲的FIFO只读将阻塞(除非我使用 os.O_NONBLOCK 与较低级别 os.open() call)。然而,如果我打开它的读/写,那么我没有阻止。



例子:

  f = open('./ myfifo','r')#块除非数据已经在管道中
f = os.open('./ myfifo',os.O_RDONLY)#同上

#对比于:
f = open('./ myfifo','w +')#不会阻止
f = os.open('./ myfifo',os .O_RDWR)#同上
f = os.open('./ myfifo',os.O_RDONLY | os.O_NONBLOCK)#同上

我只是好奇而已。为什么打开的调用块而不是一些后续的读取操作?

另外我注意到一个非阻塞文件描述符可以在Python中表现出不同的行为。在初始打开操作中使用 os.open() os.O_NONBLOCK 的情况下, code> os.read()似乎在文件描述符中没有准备好数据时会返回一个空字符串。但是,如果我使用 fcntl.fcnt(f.fileno(),fcntl.F_SETFL,fcntl.GETFL | os.O_NONBLOCK),则使用 os。阅读引发一个异常( errno.EWOULDBLOCK

通过正常的 open()来设置,这不是由我的 os.open()例子设置的。他们有什么不同,为什么?

这就是它的定义方式。从 open()的Open Group页面c $ c $> 函数

  O_NONBLOCK 

使用O_RDONLY或O_WRONLY设置:如果O_NONBLOCK是
设置:

只读的open()将立即返回。如果没有进程当前
打开文件,则仅用于写入的open()
将返回一个错误。

如果O_NONBLOCK清零:

只读的open()将阻塞调用线程,直到
线程打开文件进行写入。用于写入
的open()会阻塞调用线程,直到线程打开
读取文件。


I've noticed a couple of oddities when dealing with named pipes (FIFOs) under various flavors of UNIX (Linux, FreeBSD and MacOS X) using Python. The first, and perhaps most annoying is that attempts to open an empty/idle FIFO read-only will block (unless I use os.O_NONBLOCK with the lower level os.open() call). However, if I open it for read/write then I get no blocking.

Examples:

f = open('./myfifo', 'r')               # Blocks unless data is already in the pipe
f = os.open('./myfifo', os.O_RDONLY)    # ditto

# Contrast to:
f = open('./myfifo', 'w+')                           # does NOT block
f = os.open('./myfifo', os.O_RDWR)                   # ditto
f = os.open('./myfifo', os.O_RDONLY|os.O_NONBLOCK)   # ditto

I'm just curious why. Why does the open call block rather than some subsequent read operation?

Also I've noticed that a non-blocking file descriptor can exhibit to different behaviors in Python. In the case where I use os.open() with the os.O_NONBLOCK for the initial opening operation then an os.read() seems to return an empty string if data isn't ready on the file descriptor. However, if I use fcntl.fcnt(f.fileno(), fcntl.F_SETFL, fcntl.GETFL | os.O_NONBLOCK) then an os.read raises an exception (errno.EWOULDBLOCK)

Is there some other flag being set by the normal open() that's not set by my os.open() example? How are they different and why?

解决方案

That's just the way it's defined. From the Open Group page for the open() function

O_NONBLOCK

    When opening a FIFO with O_RDONLY or O_WRONLY set: If O_NONBLOCK is
    set:

        An open() for reading only will return without delay. An open()
        for writing only will return an error if no process currently
        has the file open for reading.

    If O_NONBLOCK is clear:

        An open() for reading only will block the calling thread until a
        thread opens the file for writing. An open() for writing only
        will block the calling thread until a thread opens the file for
        reading.

这篇关于为什么只读打开一个命名管道块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆