【Python】subprocess - 子进程管理

 

目录

一. 介绍

二. 说明

三. 实例

四. 参考


 

一. 介绍

subprocess 模块允许你生成新的进程,连接它们的输入、输出、错误管道,并且获取它们的返回码。

描述了一个用于启动进程并与进程进行通信的新模块。

 

二. 说明

在任何编程语言中,启动新流程都是常见的任务,而在高级语言(如Python)中则很常见。需要对此任务提供良好的支持,因为:

  • 不适当的函数来启动进程可能意味着安全风险:如果程序是通过Shell启动的,并且参数包含Shell元字符,则结果可能是灾难性的。[1]
  • 它使Python成为过度复杂的Shell脚本的更好的替代语言。

当前,Python具有大量用于进程创建的不同功能。这使开发人员难以选择。

 

定义:

class Popen(args, bufsize=0, executable=None,
            stdin=None, stdout=None, stderr=None,
            preexec_fn=None, close_fds=False, shell=False,
            cwd=None, env=None, universal_newlines=False,
            startupinfo=None, creationflags=0):

 

三. 实例

# -- coding:utf-8 --

import os
import subprocess


def test_popen(cmds):
    p = subprocess.Popen(cmds[0], stdout=subprocess.PIPE, shell=True)
    print 'the current sub process pid(cwd: %s, ppid: %s, id: %s%d).' % (
        p.pid, os.getppid(), type(p), id(p))
    processes = [p]
    for x in cmds[1:]:
        p = subprocess.Popen(x,
                             stdin=p.stdout,
                             stdout=subprocess.PIPE,
                             shell=True)
        print 'the current sub process pid(cwd: %s, ppid: %s, id: %s%d).' % (
            p.pid, os.getppid(), type(p), id(p))
        # print type(p), id(p)
        processes.append(p)
    print 'the current run process pid(cwd: %s, ppid: %s, id: %s%d).' % (
        p.pid, os.getppid(), type(p), id(p))
    print type(p), id(p)
    output = p.communicate()[0]
    # for p in processes:
    #     print 'the current wait pid(%s).' % p.pid
    #     p.wait()

    # print 'the current run process pid(cwd: %s, ppid: %s).' % (p.pid,
    #                                                            os.getppid())
    content = output.rstrip('\n')
    print 'the current run process pid(cwd: %s, ppid: %s, id: %s%d).' % (
        p.pid, os.getppid(), type(p), id(p))
    return content


if __name__ == "__main__":
    '''主函数入口'''
    print
    print 'the current run main process is (cwd: %s, ppid: %s).' % (
        os.getpid(), os.getppid())
    # git config --local --list | grep "user"
    # cmds = ["git config --local --list", "grep \"user\""]
    cmds = ["git config --local --list", "grep \"user\"", "grep \"@\""]
    result = test_popen(cmds)
    print 'the current run main process is (cwd: %s, ppid: %s).' % (
        os.getpid(), os.getppid())
    print 'the content is %s' % result

 

四. 参考

  1. https://www.python.org/dev/peps/
  2. https://www.python.org/dev/peps/pep-0324/
  3. https://docs.python.org/zh-cn/2.7/library/subprocess.html
  4. https://docs.python.org/zh-cn/3/library/subprocess.html
  5. https://stackoverflow.com/questions/89228/how-to-call-an-external-command

 

 

(完)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值