subprocess 模块


0 模块描述 / Module Description

From subprocess module:

"""Subprocesses with accessible I/O streams This module allows you to spawn processes, connect to their
input/output/error pipes, and obtain their return codes. For a complete description of this module see the Python documentation. Main API
========
run(...): Runs a command, waits for it to complete, then returns a CompletedProcess instance.
Popen(...): A class for flexibly executing a command in a new process Constants
---------
DEVNULL: Special value that indicates that os.devnull should be used
PIPE:    Special value that indicates a pipe should be created
STDOUT:  Special value that indicates that stderr should go to stdout Older API
=========
call(...): Runs a command, waits for it to complete, then returns the return code.
check_call(...): Same as call() but raises CalledProcessError() if return code is not 0
check_output(...): Same as check_call() but returns the contents of stdout instead of a return code
getoutput(...): Runs a command in the shell, waits for it to complete, then returns the output
getstatusoutput(...): Runs a command in the shell, waits for it to complete, then returns a (status, output) tuple
"""  

1 常量 / Constants

1.0 PIPE常量

常量数值: PIPE = -1

常量功能:一个特殊数值,表示需要创建一个pipe。将这个变量传给stdout/stdin/stderr可以实现将子进程输出传给父进程

1.1 STDOUT常量

常量数值: STDOUT = -2

常量功能:一个特殊数值,表示stderr需要转入stdout中

1.2 DEVNULL常量

常量数值: DEVNULL = -3

常量功能:一个特殊数值,表示需要使用os.devnull

2 函数 / Function

2.0 run()函数

函数调用: re = subprocess.run(*popenargs, input=None, timeout=None, check=False)

函数功能:创建新进程运行程序,返回新进程的CompletedProcess实例

传入参数: *popenargs, input, timeout, check

*popenargs: list类型,调用新进程时使用的输入

input: obj类型,用于设置新进程的输入

timeout: int类型,设置超时时间限制,若进程用时太久,会引发TimeoutExpired

check: bool类型,检测程序退出,若退出码不是0,引发CalledProecssError

返回参数: re

re: instance类型,返回的CompletedProcess实例

2.1 call()函数

函数调用: re = subprocess.call(*popenargs, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)

函数功能:创建新进程运行程序,输入输出绑定到父进程,返回新进程退出码

传入参数: *popenargs, stdin, stdout, stderr, shell, timeout

*popenargs: list类型,调用新进程时使用的输入

stdin: obj类型,用于设置新进程的输入

stdout: obj类型,用于设置新进程的输出

stderr: obj类型,用于设置新进程的错误信息

shell: bool类型,设置是否使用中间shell来执行(可以使用shell相关变量等)

timeout: int类型,设置超时时间限制

返回参数: re

re: int类型,返回的退出码,0为正常退出

Note: 对于新进程的输入参数,以list形式传入,例如命令python test.py,则传入参数列表[‘python’, ‘test.py’]。

2.2 check_call()函数

函数调用: re = subprocess.check_call(*popenargs, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)

函数功能:创建新进程运行程序,输入输出绑定到父进程,正常退出返回退出码0,否则引发一个subprocess.CalledProcessError

传入参数: *popenargs, stdin, stdout, stderr, shell, timeout

*popenargs: list类型,调用新进程时使用的输入

stdin: obj类型,用于设置新进程的输入

stdout: obj类型,用于设置新进程的输出

stderr: obj类型,用于设置新进程的错误信息

shell: bool类型,设置是否使用中间shell来执行(可以使用shell相关变量等)

timeout: int类型,设置超时时间限制

返回参数: re

re: int类型,返回的退出码,0为正常退出

2.3 getstatusoutput()函数

函数调用: re = subprocess.getstatusoutput(cmd)

函数功能:创建新进程运行程序,元组形式返回新进程退出码和输出

传入参数: cmd

cmd: list类型,调用新进程时使用的输入,[‘python’, ‘test.py’]形式

返回参数: re

re: tuple类型,返回的元组,包含退出码和输出

2.4 getoutput()函数

函数调用: re = subprocess.getoutput(cmd)

函数功能:创建新进程运行程序,字符串形式返回新进程的输出

传入参数: cmd

cmd: list类型,调用新进程时使用的输入,[‘python’, ‘test.py’]形式

返回参数: re

re: str类型,返回的子进程输出

2.5 check_output()函数

函数调用: re = subprocess.check_output(*popenargs, input=None, stdin=None,

stdout=None, stderr=None, shell=False, universal_newlines=False, timeout=None)

函数功能:创建新进程运行程序,返回新进程的输出

传入参数: *popenargs, input, stdin, stdout, stderr, shell, universal_newlines, timeout

*popenargs: list类型,调用新进程时使用的输入

Input: byte/str类型,一个额外可用的输入,允许传入一个字(b)符串给stdin

stdin: obj类型,用于设置新进程的输入

stdout: obj类型,用于设置新进程的输出

stderr: obj类型,用于设置新进程的错误信息

shell: bool类型,设置是否使用中间shell来执行(可以使用shell相关变量等)

universal_newlines: bool类型,设置输入输出的格式,False为byte,True为str

timeout: int类型,设置超时时间限制

返回参数: re

re: byte/str类型,返回的输出结果

3 / Class

3.1 Popen

类实例化:prcs = subprocess.Popen(args, stdin=None, stdout=None, stderr=None, […])

类的功能:用于生成一个新进程执行子程序

传入参数: args

args: list类型,新进程执行的输入

stdin: obj/int类型,用于设置新进程的输入

stdout: obj/int类型,用于设置新进程的输出

stderr: obj/int类型,用于设置新进程的错误信息

返回参数: prcs

prcs: instance类型,生成的新进程实例

Note: 对于新进程的输入参数args,以list形式传入,例如命令python test.py,则传入参数列表[‘python’, ‘test.py’]。

3.1.1 pid属性

属性调用: pid = prcs.pid

属性功能: 返回子进程的pid信息

属性参数: pid

pid: int类型,子进程的pid

3.1.2 communicate()方法

函数调用: re = prcs.communicate(input=None, timeout=None)

函数功能:用于进程之间通信,发送数据到stdin,并从stdout和stderr读取数据

传入参数: input, timeout

input: byte/str类型,一个额外可用的输入,允许传入一个字(b)符串给stdin

timeout: int类型,设置超时时间限制

返回参数: re

re: tuple类型,返回的输出结果,(stdout, stderr)

3.1.3 poll()方法

函数调用: re = prcs.poll()

函数功能:用于检测子进程是否结束

传入参数:

返回参数: re

re: int类型,返回的结果,为1则子进程已结束

3.2 CompletedProcess

类实例化:re = subprocess.run() / CompletedProcess(args, returncode, stdout=None, stderr=None)

类的功能:一个已经完成运行的子进程,通常为调用subprocess.run()函数时返回生成

传入参数: args, returncode, stdout, stderr

args: list类型,新进程执行的输入

returncode: int类型,子进程的退出码

stdout: obj/NoneType类型,子进程的输出,如果没获取到则为None

stderr: obj/NoneType类型,子进程的错误信息,如果没获取到则为None

返回参数: re

re: instance类型,生成的已结束子进程实例

转载于:https://www.cnblogs.com/stacklike/p/8166950.html

Python的并发并行[3] - 进程[0] - subprocess 模块相关推荐

  1. Python的并发并行[3] - 进程[1] - 多进程的基本使用

    多进程的基本使用 1 subprocess 常用函数示例 首先定义一个子进程调用的程序,用于打印一个输出语句,并获取命令行参数 1 import sys 2 print('Called_Functio ...

  2. python concurrent queue_Python的并发并行[2] - 队列[0] - queue 模块

    queue模块/ queue Module 1常量/ Constants Pass 2函数/ Function Pass 3类/ Class 3.1 Queue类 类实例化:queue= queue. ...

  3. python爬虫并发并行下载

    1一百万个网站 1用普通方法解析Alexa列表 2复用爬虫代码解析Alexa列表 2串行爬虫 3并发并行爬虫 0并发并行工作原理 1多线程爬虫 2多进程爬虫 4性能对比 这篇将介绍使用多线程和多进程这 ...

  4. java基础巩固-宇宙第一AiYWM:为了维持生计,四大基础之OS_Part_1整起(进程线程协程并发并行、进程线程切换进程间通信、死锁\进程调度策略、分段分页、交换空间、OS三大调度机制)

    PART0:OS,这货到底是个啥? OS,是个啥? OS的结构们: 存储器: 存储器的层次结构: 内存:我们的程序和数据都是存储在内存,我们的程序和数据都是存储在内存,每一个字节都对应一个内存地址.内 ...

  5. Python的并发并行[1] - 线程[3] - 多线程的同步控制

    多线程的控制方式 目录 唤醒单个线程等待 唤醒多个线程等待 条件函数等待 事件触发标志 函数延迟启动 设置线程障碍 1 唤醒单个线程等待 Condition类相当于一把高级的锁,可以进行一些复杂的线程 ...

  6. 简单理解socket(socket.AF_INET,socket.SOCK_STRE,os.dup2(s.fileno(),0),subprocess.call([“/bin/bash“,“-i“])

    前言:因为最近学习渗透时,使用python编写了一个木马,但是对里面的函数/方法功能并不了解,于是查阅资料,总算有了一定了解,于是记录下来 木马文件如下: import socket,subproce ...

  7. python subprocess模块_python subprocess模块详解

    一.subprocess标准库 python执行shell脚本,通常会使用so模块中的几个方法,如system.spawn*.popen等.subprocess标准库的引入就是为了取代这些老的模块方法 ...

  8. python并发处理list数据_python之并发基础(进程)

    今日内容: 操作系统发展史 多道技术 进程理论 开启进程的两种方式 进程对象的join方法 进程之间数据的相互隔离 进程对象的其他方法 僵尸进程和孤儿进程 守护进程 互斥锁 队列介绍 进程间通信IPC ...

  9. 学习笔记(18):Python网络编程并发编程-守护进程

    立即学习:https://edu.csdn.net/course/play/24458/296429?utm_source=blogtoedu 守护进程(了解) 1.概念:守护进程是主进程在创建子进程 ...

  10. python并发编程之semaphore(信号量)_浅谈Python并发编程之进程(守护进程、锁、信号量)...

    前言:本博文是对Python并发编程之进程的知识延伸,主要讲解:守护进程.锁.信号量. 友情链接: 一.守护进程(daemon) 1.1 守护进程概念 首先我们都知道:正常情况下,主进程默认等待子进程 ...

最新文章

  1. SAP创建webservice
  2. SAP WM 共用同一个仓库号的2个存储地点之间转库
  3. 庆祝自己在博客园开通个人生涯第一个关于工作的博客
  4. JavaScript 技术篇-js检测原生对象类型实例演示,js的3种对象类型
  5. Java五道输出易错题解析(进来挑战下)
  6. loadrunner—事务、TPS
  7. OD的hit跟踪和run跟踪
  8. redis-4.0.11主从配置初步探究
  9. (转)FPGA的速度等级(speed grade)
  10. 面向区块链的高效物化视图维护和可信查询论文学习
  11. 通用智能传感集线器(Sensorhub)介绍
  12. 斗地主AI算法——第十章の被动出牌(4)
  13. python制作软件界面_Python 脚本 GUI 界面生成工具
  14. Navicat MySQL连接Linux下MySQL的及2003错误解决方案
  15. python模块搜索原则_python 从小白开始 - 模块,包以及路径搜索
  16. php adodb类库下载,PHP ADODB连接、操作数据库类
  17. logistic回归分析优点_逻辑回归算法的优缺点
  18. 如何用计算机算十进制,计算器怎么,计算机是怎么转换二进制为十进制的
  19. 网站不能正常访问的原因及几处处理方法
  20. vue/cle3项目运行报错sockjs-node/info解决方案

热门文章

  1. 【渝粤教育】国家开放大学2018年春季 0008-22T简明现代汉语 参考试题
  2. 谷歌大脑阿尔伯塔联合发表:离线强化学习的优化视角【附代码】
  3. 计算机视觉实战(二)图像基本操作
  4. String、Object、包装类的常用方法以及注意点
  5. Bootstrap table的基础用法
  6. 这些Python骚操作,你知道吗?
  7. 读书笔记_Effective C++_条款一:将C++视为一个语言联邦
  8. 【转】容器 C++ set和map
  9. 【LeetCode】盛最多水的容器【双指针+贪心 寻找最大面积】
  10. 解决微信小程序Video 某些属性设置不起作用问题