Using subprocess.Popen for Process with Large Output

后端 未结 7 1971
礼貌的吻别
礼貌的吻别 2020-12-02 16:56

I have some Python code that executes an external app which works fine when the app has a small amount of output, but hangs when there is a lot. My code looks like:

7条回答
  •  萌比男神i
    2020-12-02 17:46

    Here is simple approach which captures both regular output plus error output, all within Python so limitations in stdout don't apply:

    com_str = 'uname -a'
    command = subprocess.Popen([com_str], stdout=subprocess.PIPE, shell=True)
    (output, error) = command.communicate()
    print output
    
    Linux 3.11.0-20-generic SMP Fri May 2 21:32:55 UTC 2014 
    

    and

    com_str = 'id'
    command = subprocess.Popen([com_str], stdout=subprocess.PIPE, shell=True)
    (output, error) = command.communicate()
    print output
    
    uid=1000(myname) gid=1000(mygrp) groups=1000(cell),0(root)
    

提交回复
热议问题