简体   繁体   中英

python multiprocessing pipe poll bug

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:

  • other functions of the same name in different contexts, like the system call 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
  • returning True from the method can be understand as saying that subsequent recv will not block - exactly the case here
  • calling 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM