select() on pipe

B

Brad Murdoch

im trying to run the select.selct() on a unix pipe, my expectation is
that it will block untill there is something there to read, then
continue. It seems to do this untill the pipe is written to once, then i
get a busy while loop.
shouldnt this stop each time, to wait for something to be written to
the pipe. obviously im a novice when it comes to python programming,
what am i missing here.

cheers..

#!/usr/bin/python

import os
import sys
import select

try:
fifo = open('test.fifo','r')
except Exception, blah:
report = "ERROR 001: " + str(blah)
print report
sys.exit(2)

while 1:
line = False
r,w,x = select.select([fifo],[],[])
if r:
line=fifo.read()
print line
 
D

Donn Cave

Brad Murdoch said:
im trying to run the select.selct() on a unix pipe, my expectation is
that it will block untill there is something there to read, then
continue. It seems to do this untill the pipe is written to once, then i
get a busy while loop.
shouldnt this stop each time, to wait for something to be written to
the pipe. obviously im a novice when it comes to python programming,
what am i missing here.

try:
fifo = open('test.fifo','r')
while 1:
line = False
r,w,x = select.select([fifo],[],[])
if r:
line=fifo.read()
print line


Well, two things, neither of them really unique to
Python. The immediate problem is the semantics of
pipe I/O, specifically "end of file." For pipes
and other IPC devices, end of file means that the
the other end of the file has been closed, which
your "the pipe is written to" event probably does.
From this point on, read(2) returns 0 bytes, which
means end of file. If you want to go back and do
this again, you need to re-open the pipe.

The second problem will bite you as soon as you get
the first part working: fifo.read() is going to
block until the pipe closes. If you switch to
fifo.readline(), select may start failing to detect
data that readline() buffers. For more reliable
I/O in conjunction with select(), use POSIX I/O:

fifd = os.open('test.fifo', os.O_RDONLY)
...
while 1:
...
data = os.read(fifd, 8192)

Donn Cave, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,236
Messages
2,571,185
Members
47,820
Latest member
HortenseKo

Latest Threads

Top