Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python open fifo blocks forever

I'm trying to implement IPC using named pipes in Python, but there is a problem. open blocks the process

import os
path = '/tmp/fifo'
os.mkfifo(path)
fifo = open(path, 'w') # never returns

Same with open(path, 'r')

What am I doing wrong?

Python 3.6.1

like image 896
Oleg Antonyan Avatar asked Jul 21 '26 18:07

Oleg Antonyan


2 Answers

Figured this out. open blocks until the pipe is open on the other side

With os.open, the statement will be nonblocking if os.O_NONBLOCK is given as a flag. Nonblocking may not work on your OS. I believe it works on Unix distros but not Windows.

like image 176
Oleg Antonyan Avatar answered Jul 23 '26 09:07

Oleg Antonyan


Correction to the accpeted answer, which is too inaccurate, if not totally wrong. Below is from man fifo(7) of Linux:

First, blocking mode opening. This is what OP found:

Normally, opening the FIFO blocks until the other end is opened also.

Non-blocking, read-only opening case is:

A process can open a FIFO in nonblocking mode. In this case, opening for read-only succeeds even if no one has opened on the write side yet

But non-blocking, write-only opening is dangerous:

and opening for write-only fails with ENXIO (no such device or address) unless the other end has already been opened.

There is a workaround, namely read-write opening. But this also needs care:

Under Linux, opening a FIFO for read and write will succeed both in blocking and nonblocking mode. POSIX leaves this behavior undefined. This can be used to open a FIFO for writing while there are no readers available.

A practical caution follows in the manual page :)

A process that uses both ends of the connection in order to communicate with itself should be, very careful to avoid deadlocks.

like image 28
teika kazura Avatar answered Jul 23 '26 09:07

teika kazura



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!