pipe large amount of data to stdin while using subprocess.Popen

后端 未结 10 647
花落未央
花落未央 2020-12-08 11:08

I\'m kind of struggling to understand what is the python way of solving this simple problem.

My problem is quite simple. If you use the follwing code it will hang. T

10条回答
  •  余生分开走
    2020-12-08 11:58

    If you want a pure Python solution, you need to put either the reader or the writer in a separate thread. The threading package is a lightweight way to do this, with convenient access to common objects and no messy forking.

    import subprocess
    import threading
    import sys
    
    proc = subprocess.Popen(['cat','-'],
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            )
    def writer():
        for i in range(100000):
            proc.stdin.write(b'%d\n' % i)
        proc.stdin.close()
    thread = threading.Thread(target=writer)
    thread.start()
    for line in proc.stdout:
        sys.stdout.write(line.decode())
    thread.join()
    proc.wait()
    

    It might be neat to see the subprocess module modernized to support streams and coroutines, which would allow pipelines that mix Python pieces and shell pieces to be constructed more elegantly.

提交回复
热议问题