writing large amount of data to stdin

前端 未结 2 1320
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 23:25

I am writing a large amount of data to stdin.

How do i ensure that it is not blocking?

p=subprocess.Popen([path],stdout=subprocess.PIPE,stdin=subpr         


        
2条回答
  •  不知归路
    2020-12-07 00:04

    To avoid the deadlock in a portable way, write to the child in a separate thread:

    #!/usr/bin/env python
    from subprocess import Popen, PIPE
    from threading import Thread
    
    def pump_input(pipe, lines):
        with pipe:
            for line in lines:
                pipe.write(line)
    
    p = Popen(path, stdin=PIPE, stdout=PIPE, bufsize=1)
    Thread(target=pump_input, args=[p.stdin, lines]).start()
    with p.stdout:
        for line in iter(p.stdout.readline, b''): # read output
            print line,
    p.wait()
    

    See Python: read streaming input from subprocess.communicate()

提交回复
热议问题