Very large input and piping using subprocess.Popen

前端 未结 5 715
北恋
北恋 2020-12-25 15:40

I have pretty simple problem. I have a large file that goes through three steps, a decoding step using an external program, some processing in python, and then recoding usi

5条回答
  •  一整个雨季
    2020-12-25 16:13

    Trying to do some basic shell piping with very large input in python:

    svnadmin load /var/repo < r0-100.dump
    

    I found the simplest way to get this working even with large (2-5GB) files was:

    subprocess.check_output('svnadmin load %s < %s' % (repo, fname), shell=True)
    

    I like this method because it's simple and you can do standard shell redirection.

    I tried going the Popen route to run a redirect:

    cmd = 'svnadmin load %s' % repo
    p = Popen(cmd, stdin=PIPE, stdout=PIPE, shell=True)
    with open(fname) as inline:
        for line in inline:
            p.communicate(input=line)
    

    But that broke with large files. Using:

    p.stdin.write() 
    

    Also broke with very large files.

提交回复
热议问题