Python subprocess readlines() hangs

后端 未结 4 1830
独厮守ぢ
独厮守ぢ 2020-11-22 01:43

The task I try to accomplish is to stream a ruby file and print out the output. (NOTE: I don\'t want to print out everything at once)

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 01:51

    Basically what you are looking at here is a race condition between your proc.poll() and your readline(). Since the input on the master filehandle is never closed, if the process attempts to do a readline() on it after the ruby process has finished outputting, there will never be anything to read, but the pipe will never close. The code will only work if the shell process closes before your code tries another readline().

    Here is the timeline:

    readline()
    print-output
    poll()
    readline()
    print-output (last line of real output)
    poll() (returns false since process is not done)
    readline() (waits for more output)
    (process is done, but output pipe still open and no poll ever happens for it).
    

    Easy fix is to just use the subprocess module as it suggests in the docs, not in conjunction with openpty:

    http://docs.python.org/library/subprocess.html

    Here is a very similar problem for further study:

    Using subprocess with select and pty hangs when capturing output

提交回复
热议问题