Python open fifo blocks forever

Viewed 3550

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

3 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.

the function os.mkfifo(path) create the path is needed (if all the folder does not exists it build all the path).

so, you need to add the file name like this: fifo = open(path + file, 'r')

Generally, FIFOs are used as rendezvous between “client” and “server” type processes: the server opens the FIFO for reading, and the client opens it for writing. Note that mkfifo() doesn’t open the FIFO — it just creates the rendezvous point

According to os.mkfifo, you need to use fifo = open(path, dir_fd='w')

Related