Python的并发并行[3] -> 进程[0] -> subprocess 模块
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类型,生成的已结束子进程实例
Python的并发并行[3] -> 进程[0] -> subprocess 模块的更多相关文章
- Python的并发并行[1] -> 线程[0] -> threading 模块
threading模块 / threading Module 1 常量 / Constants Pass 2 函数 / Function 2.1 setprofile()函数 函数调用: thread ...
- Python的并发并行[2] -> 队列[0] -> queue 模块
queue 模块 / queue Module 1 常量 / Constants Pass 2 函数 / Function Pass 3 类 / Class 3.1 Queue类 类实例化:queue ...
- Python的并发并行[3] -> 进程[1] -> 多进程的基本使用
多进程的基本使用 1 subprocess 常用函数示例 首先定义一个子进程调用的程序,用于打印一个输出语句,并获取命令行参数 import sys print('Called_Function.py ...
- Python之路(第三十七篇)并发编程:进程、multiprocess模块、创建进程方式、join()、守护进程
一.在python程序中的进程操作 之前已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创建的.因此,运行起来的python程序 ...
- 网络基础之 并发编程之进程,多路复用,multiprocess模块
并发 1. 背景知识 2. 什么是进程 3. 进程调度 4. 并发与并行 5 同步\异步\阻塞\非阻塞(重点) 6.multiprocess模块 7.僵尸进程与孤儿进程 1.背景知识 一操作系统的作用 ...
- python网络编程--粘包解决方案 和 subprocess模块
1.缓冲区:作用:将程序和网络解耦分为输入缓冲区, 输出缓冲区 每个 socket 被创建后,都会分配两个缓冲区,输入缓冲区和输出缓冲区.write()/send() 并不立即向网络中传输数据,而是先 ...
- Python的并发并行[0] -> 基本概念
基本概念 / Basic Concept 快速跳转 进程 / Process 线程 / Thread 协程 / Coroutine 全局解释器锁 / Global Interpreter Lock ...
- Python 之并发编程之进程上(基本概念、并行并发、cpu调度、阻塞 )
一: 进程的概念:(Process) 进程就是正在运行的程序,它是操作系统中,资源分配的最小单位. 资源分配:分配的是cpu和内存等物理资源 进程号是进程的唯一标识 同一个程序执行两次之后是两个进程 ...
- Python的并发并行[4] -> 并发[0] -> 利用线程池启动线程
利用线程池启动线程 submit与map启动线程 利用两种方式分别启动线程,同时利用with上下文管理来对线程池进行控制 from concurrent.futures import ThreadPo ...
随机推荐
- 《算法》C++代码 快速排序
快速排序,简称快排,常称QuickSort.QSort.在排序算法中非常常用,其编程复杂度低,时间复杂度O(NlogN),空间复杂度O(N),执行效率稳定,而且常数很低. 基本思想就是二分,例如你要将 ...
- [转]赵桐正thinkphp教程笔记
原文:赵桐正thinkphp教程笔记 ,有修改 常用配置 常用配置config.php: <?php return array( //'配置项'=>'配置值' 'URL_PATHINFO_ ...
- 【Python-遇到的Error】AttributeError: 'str' object has no attribute 'input_text'
学习类的实例化的时候遇到了AttributeError: 'str' object has no attribute 'input_text', 以下是报错的代码及修改正确的代码. class shu ...
- IDEA调试快捷键
F9 resume programe 恢复程序 F8 Step Over 相当于eclipse的f6 跳到下一步 Ctrl+Shift+F,全局查 ...
- python作业:购物车(第二周)
一.作业需求: 1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4 ...
- 团队冲刺Alpha(七)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- 清除浮动float (:after方法)
1. 什么时候需要清除浮动?清除浮动有哪些方法? (1)对元素进行了浮动(float)后,该元素就会脱离文档流,浮动在文档之上.在CSS中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是 ...
- java实现远程开机
import java.io.IOException; import java.net.*;public class 远程开机 { public static void main(String[] a ...
- 【bzoj4378】[POI2015]Logistyka 离散化+树状数组
题目描述 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进行s次操作.每次 ...
- [洛谷P1501][国家集训队]Tree II
题目大意:给一棵树,有四种操作: $+\;u\;v\;c:$将路径$u->v$区间加$c$ $-\;u_1\;v_1\;u_2\;v_2:$将边$u_1-v_1$切断,改成边$u_2-v_2$, ...