python 打开一个新进程执行系统命令, test 执行完才能获取返回, test1 实时获取返回结果

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")

 

相关文章:

  • 2021-07-22
  • 2021-08-08
  • 2022-02-23
  • 2021-10-16
  • 2021-11-24
  • 2022-12-23
  • 2021-12-03
猜你喜欢
  • 2022-01-09
  • 2022-12-23
  • 2021-07-24
  • 2021-05-27
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
相关资源
相似解决方案