Using pipes for process communication with the multiprocessing library I noticed some strange behaviour of the poll function. If I close the other end of the pipe poll() returns true which is kind of odd. The python docs dont really tell what to expect. Still I was thinking poll() would at least return false if there is definitely nothing in the pipe and the other end is even closed. I use python3.3.2 but it seems to be the same for python 2.7.5. Is this intended or a bug and if it´s not a bug what is it good for?
import multiprocessing
if __name__ == '__main__':
con = multiprocessing.Pipe(True)
con1, con2 = con
print(str(con1.poll())) #prints False
con2.close()
con1.close()
con = multiprocessing.Pipe(True)
con1, con2 = con
con2.close()
print(str(con1.poll())) #prints True
con1.close()
I don't think this is a bug. I agree, the documentation is not clear in this respect, but there are several reasons why this behavior should be expected, and the opposite would cause more harm than good:
poll
on sockets/file descriptors, do the same; closing the other end is an event on the pipe, so poll
should indicate there is something to be done on this side too.True
from the method can be understood as saying that subsequent recv
will not block - exactly the case here.poll
with a non-zero timeout or with None
would mean blocking even when there is nothing to wait for, when the other side has closed.Also note that there would be no good means of detecting that the other end closed, if poll
returned False
.