subprocess.Popen stdout redirect content real-time access

python open a new process order execution system, test execution in order to obtain complete returns, test1 real-time access return results

import subprocess

def test(cmd):
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    lines = p.stdout.readlines()
    for line in lines:
        tmp = line.decode('gbk').strip()
        print(tmp)

def test1(cmd):
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    for i in iter(p.stdout.readline, ''):
        if len(i) < 1:
            break
        print(i.decode('gbk').strip())

if __name__ == '__main__':
    test("ping www.baidu.com")
    test1("ping www.baidu.com")

 

Guess you like

Origin www.cnblogs.com/6min/p/11271476.html