How to read from an os.pipe() without getting blocked?

后端 未结 1 754
太阳男子
太阳男子 2021-02-04 14:45

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

相关标签:
1条回答
  • 2021-02-04 15:16

    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) man page:

    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.

    0 讨论(0)
提交回复
热议问题