• subprocess.Popen() 常用方法


    p.stdout.read() :用于读取标准输出,会一次性读取所有内容,返回一个字符串
    p.stdout.readline() :用于读取标准输出,一次只读取一行内容,返回一个字符串
    p.stdout.readlines() :用于读取标准输出,一次性读取所有内容,返回一个列表,每一行是列表的一个元素

    from subprocess import Popen, PIPE
    
    p = Popen('ls /data', stdout=PIPE, shell=True)
    for line in p.stdout.readlines():
        print(line),
    [root@localhost ~]$ python 1.py
    1.txt
    2.txt
    3.txt

    p.wait() :等待子进程结束,并返回状态码。如下,如果没有加上 p.wait(),则 sleep 100 还没有执行完,就会执行 print('End'),如果加上就会等待 sleep 100 执行完

    from subprocess import Popen, PIPE
    
    p = Popen('sleep 100', stdout=PIPE, shell=True)
    p.wait()
    print('End')

    p.pid :用于查看子进程的PID

    from subprocess import Popen, PIPE
    
    p = Popen('sleep 100', stdout=PIPE, shell=True)
    print(p.pid)
    [root@localhost ~]$ python 1.py
    35327
    [root@localhost ~]$ ps aux | grep 35327
    root      35327  0.0  0.0 107904   612 pts/0    S    17:56   0:00 sleep 100
    root      35329  0.0  0.0 112676   984 pts/0    S+   17:57   0:00 grep --color=auto 35327

    p.poll() :用于检查子进程(命令)是否已经执行结束,没结束返回None,结束后返回状态码

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    import time
    from subprocess import Popen, PIPE
    
    p = Popen('sleep 3', stdout=PIPE, shell=True)
    
    while True:
        if p.poll() == None:
            print('程序执行中...')
            time.sleep(1)
        else:
            print('程序执行完毕, 状态码为:%s' % p.poll())
            break
    [root@localhost ~]$ python 1.py
    程序执行中...
    程序执行中...
    程序执行中...
    程序执行完毕, 状态码为:0

    p.returncode :用于返回命令执行完之后的状态码

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    import time
    from subprocess import Popen, PIPE
    
    p = Popen('sleep 3', stdout=PIPE, shell=True)
    
    while True:
        if p.poll() == None:
            print('程序执行中...')
            time.sleep(1)
        else:
            print('程序执行完毕, 状态码为:%s' % p.returncode)
            break
    [root@localhost ~]$ python 1.py
    程序执行中...
    程序执行中...
    程序执行中...
    程序执行完毕, 状态码为:0

    p.kill() :用于杀死子进程

    from subprocess import Popen, PIPE
    
    p = Popen('sleep 100', stdout=PIPE, shell=True)
    print(p.pid)
    p.kill()
    [root@localhost ~]$ python 1.py
    35403
    [root@localhost ~]$ ps aux | grep 35403   # 可以看到子进程已经不在了
    root      35405  0.0  0.0 112676   980 pts/0    S+   18:12   0:00 grep --color=auto 35403

    p.terminate() :用于终止子进程,与 kill() 差不多

    from subprocess import Popen, PIPE
    
    p = Popen('sleep 100', stdout=PIPE, shell=True)
    print(p.pid)
    p.terminate()
    [root@localhost ~]$ python 1.py
    35403
    [root@localhost ~]$ ps aux | grep 35403   # 可以看到子进程已经不在了
    root      35405  0.0  0.0 112676   980 pts/0    S+   18:12   0:00 grep --color=auto 35403

    p.communicate() :该方法可用来与子进程进行交互,比如发送数据到stdin,结果会返回一个元组,这个元组包含stdout和stderr

    from subprocess import Popen, PIPE
    
    p = Popen('cat', stdin=PIPE, stdout=PIPE, shell=True)
    print p.communicate('hello world')
    [root@localhost ~]$ python 1.py
    ('hello world', None)

        

  • 相关阅读:
    python thrift
    redis 知识点
    Spring其他注解和xml配置(不常用的)
    Spring常用的的注解以及对应xml配置详解
    Eureka的工作原理简介
    SpringBoot的自动配置实现和介绍
    SpringBoot多配置文件,切换环境
    数据卷介绍和常用的服务部署
    Spring Security简介
    在Java中入门,读取和创建Excel,Apache POI的使用
  • 原文地址:https://www.cnblogs.com/pzk7788/p/10305040.html
Copyright © 2020-2023  润新知