Python subprocess readlines() hangs

后端 未结 4 1885
独厮守ぢ
独厮守ぢ 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 02:03

    Try this:

    proc = Popen(command, bufsize=0, shell=True, stdout=PIPE, close_fds=True)
    for line in proc.stdout:
        print line
    
    print("This is most certainly reached!")
    

    As others have noted, readline() will block when reading data. It will even do so when your child process has died. I am not sure why this does not happen when executing ls as in the other answer, but maybe the ruby interpreter detects that it is writing to a PIPE and therefore it will not close automatically.

提交回复
热议问题