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?
Read/Write Are Blocking - when a process reads from a named pipe that has no data in it, the reading process is blocked. It does not receive an end of file (EOF) value, like when reading from a file.
Named pipes can also be used in Un-Blocked mode i.e., the sender (receiver) process will work independently of the other process.
Both reading and writing are by default blocking. This means that if a process tries to read from a named-pipe that does not have data, it will block. Similarly, if a process write into a named-pipe that has not yet been opened by another process, the writer will block.
A named pipe is a one-way or duplex pipe that provides communication between the pipe server and some pipe clients. A pipe is a section of memory that is used for interprocess communication. A named pipe can be described as first in, first out (FIFO); the inputs that enter first will be output first.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With