zoukankan      html  css  js  c++  java
  • subprocess模块

    一、subprocess介绍:

    subprocess模块中只定义了一个类: Popen。可以使用Popen来创建进程,并与进程进行复杂的交互。

    1、使用subprocess包中的函数创建子进程的时候,要注意:

    1、在创建子进程之后,父进程是否暂停,并等待子进程运行。
    2、函数返回什么?
    3、当returncode不为0时,父进程如何处理。

    2、subprocess.call()

    例:
    import  subprocess
    ping = subprocess.call('ping 8.8.8.8',shell=True)
    print(ping)
    print('hello')
    
    父进程等待子进程完成,返回退出信息(0为执行成功,其他为错误)注:在windows
    会乱码。

    3、subprocess.check_call()

    父进程等待子进程完成
    检查退出信息,如果返回值不为0,则举出错误subprocess.CalledProcessError,可以用try抓到错误。

     4、subprocess.check_output()

    父进程等待子进程完成
    返回子进程向标准输出的输出结果
    检查退出信息,如果返回值不为0,则举出错误subprocess.CalledProcessError,可用try来检查。

     5、Popen()

    实际上,我们上面的三个函数都是基于Popen()的封装(wrapper)。这些封装的目的在于让我们容易使用子进程。当我们想要更个性化我们的需求的时候,就要转向Popen类,该类生成的对象用来代表子进程。

    与上面的封装不同,Popen对象创建后,主程序不会自动等待子进程完成。我们必须调用对象的wait()方法,父进程才会等待 (也就是阻塞block):

    1、不等待子进程
    import subprocess
    child = subprocess.Popen(["ping","-c","5","www.google.com"])
    print("parent process")
    2、等待子进程
    import subprocess
    child = subprocess.Popen(["ping","-c","5","www.google.com"])
    child.wait()
    print("parent process")

    此外,你还可以在父进程中对子进程进行其它操作,比如我们上面例子中的child对象:

    obj.poll()         #检查子进程状态
    obj.kill()         #终止子进程
    obj.send_signal()  #向子进程发送信号
    obj.terminate()    #终止子进程
    obj.terminate()    #停止(stop)子进程。在windows平台下,该方法将调用Windows API TerminateProcess() #来结束子进程。
    obj.kill()         #杀死子进程。
    obj.pid            #获取子进程的进程ID。
    obj.returncode     #获取进程的返回值。如果进程还没有结束,返回None 

     以下是运行系统命令,出现异常会提示是否退出,并记录日志:

    import subprocess,sys,os
    import logging
    def Logger(method,file):
        logger = logging.getLogger(method)
        formatter = logging.Formatter('%(asctime)s %(levelname)-8s:%(message)s')
        file_handler = logging.FileHandler(file)
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)
        logger.setLevel(logging.INFO)
        return logger
    def judge_exit():
        while True:
            choice = input("是否继续运行(Y/N):")
            if choice == 'Y' or choice == 'y':
                break
            elif choice == 'N' or choice == 'n':
                sys.exit(1)
    def run_sys_command(command):
        logger = Logger('run_sys_command', './log/controller_install.log')
        if 'systemctl enable' in command:
            os.system(command)
        else:
            obj = subprocess.Popen([command], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,universal_newlines=True)
    #        obj.wait()
            std_out,std_err = obj.stdout.read(), obj.stderr.read()
            print(command)
            if len(std_out) != 0:
                print(std_out)
            if len(std_err) != 0:
                logger.error("%s<==>%s" % (command, std_err))
                print(std_err)
                judge_exit()
    注:subprocess.Popen执行(systemctl enable)时,会认为执行错误。

    子进程的文本流控制

    (沿用child子进程) 子进程的标准输入,标准输出和标准错误也可以通过如下属性表示:

    child.stdin

    child.stdout

    child.stderr

    我们可以在Popen()建立子进程的时候改变标准输入、标准输出和标准错误,并可以利用subprocess.PIPE将多个子进程的输入和输出连接在一起,构成管道(pipe):

    import subprocess
    child1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE)
    child2 = subprocess.Popen(["wc"], stdin=child1.stdout,stdout=subprocess.PIPE)
    out = child2.communicate()
    print(out)

    subprocess.PIPE实际上为文本流提供一个缓存区。child1的stdout将文本输出到缓存区,随后child2的stdin从该PIPE中将文本读取走。child2的输出文本也被存放在PIPE中,直到communicate()方法从PIPE中读取出PIPE中的文本。

    参考:https://www.cnblogs.com/vamei/archive/2012/09/23/2698014.html

  • 相关阅读:
    NBU备份虚拟机高级选项virtual machine quiesce option
    find命令处理之exec与xargs区别
    python学习之列表
    vSphere 6.5密码正确不能登录解决方法
    Vmware vSphere P2V操作文档
    tomcat学习1架构理解
    telnet 退出命令
    敏捷软件开发
    数组、List和ArrayList的区别
    如何阅读一本书(感)
  • 原文地址:https://www.cnblogs.com/chimeiwangliang/p/7865503.html
Copyright © 2011-2022 走看看