views:

379

answers:

1

I'm trying to read from an open os.pipe() to see if it's empty at the moment of the reading. The problem is that calling read() causes the program to block there until there is actually something to read there however there won't be any, if the test I'm doing succeeded.

I know I can use select.select() with a timeout however I wanted to know if there is another solution to the problem. In the meanwhile I'll use select.select()

+4  A: 

You might try this.

import os, fcntl
fcntl.fcntl(thePipe, fcntl.F_SETFL, os.O_NONBLOCK)

With this thePipe.read() should be non-blocking.

From pipe(7) manpage:

If a process attempts to read from an empty pipe, then read(2) will block until data is available. (...) Non-blocking I/O is possible by using the fcntl(2) F_SETFL operation to enable the O_NONBLOCK open file status flag.

vartec
Thank you very much. This is exactly what I needed.
mpeterson