Python subprocess readlines() hangs

后端 未结 4 1894
独厮守ぢ
独厮守ぢ 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:12

    Not sure what is wrong with your code, but the following seems to work for me:

    #!/usr/bin/python
    
    from subprocess import Popen, PIPE
    import threading
    
    p = Popen('ls', stdout=PIPE)
    
    class ReaderThread(threading.Thread):
    
        def __init__(self, stream):
            threading.Thread.__init__(self)
            self.stream = stream
    
        def run(self):
            while True:
                line = self.stream.readline()
                if len(line) == 0:
                    break
                print line,
    
    
    reader = ReaderThread(p.stdout)
    reader.start()
    
    # Wait until subprocess is done
    p.wait()
    
    # Wait until we've processed all output
    reader.join()
    
    print "Done!"
    

    Note that I don't have Ruby installed and hence cannot check with your actual problem. Works fine with ls, though.

提交回复
热议问题