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

后端 未结 10 623
花落未央
花落未央 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 12:04

    Here is an example (Python 3) of reading one record at a time from gzip using a pipe:

    cmd = 'gzip -dc compressed_file.gz'
    pipe = Popen(cmd, stdout=PIPE).stdout
    
    for line in pipe:
        print(":", line.decode(), end="")
    

    I know there is a standard module for that, it is just meant as an example. You can read the whole output in one go (like shell back-ticks) using the communicate method, but obviously you hav eto be careful of memory size.

    Here is an example (Python 3 again) of writing records to the lp(1) program on Linux:

    cmd = 'lp -'
    proc = Popen(cmd, stdin=PIPE)
    proc.communicate(some_data.encode())
    

提交回复
热议问题