襄樊网站建设公司,软件开发工程师面试自我介绍,中国景观设计网,汽车租赁网站开发subprocess模块 通过Python去执行一条系统命令或脚本。 三种执行命令的方法 subprocess.run(*popenargs, inputNone, timeoutNone, checkFalse, **kwargs) #官方推荐 subprocess.call(*popenargs, timeoutNone, **kwargs) #跟上面实现的内容差不多#xff0c;另一种写法 sub…subprocess模块 通过Python去执行一条系统命令或脚本。 三种执行命令的方法 subprocess.run(*popenargs, inputNone, timeoutNone, checkFalse, **kwargs) #官方推荐 subprocess.call(*popenargs, timeoutNone, **kwargs) #跟上面实现的内容差不多另一种写法 subprocess.Popen() #上面各种方法的底层封装 run方法 标准写法 subprocess.run([df,-h],stderrsubprocess.PIPE,stdoutsubprocess.PIPE,checkTrue)
#checkTrue代表如果命令出现错误程序会抛出异常 涉及到管道|的命令需要这样写 subprocess.run(df -h|grep disk1,shellTrue) #shellTrue的意思是这条命令直接交给系统去执行不需要python负责解析 call方法 #执行命令返回命令执行状态 0 or 非0retcode subprocess.call([ls, -l])#执行命令如果命令结果为0就正常返回否则抛异常subprocess.check_call([ls, -l])
0#接收字符串格式命令返回元组形式第1个元素是执行状态第2个是命令结果 subprocess.getstatusoutput(ls /bin/ls)
(0, /bin/ls)#接收字符串格式命令并返回结果subprocess.getoutput(ls /bin/ls)
/bin/ls#执行命令并返回结果注意是返回结果不是打印下例结果返回给resressubprocess.check_output([ls,-l])res
btotal 0\ndrwxr-xr-x 12 alex staff 408 Nov 2 11:05 OldBoyCRM\n Popen方法 常用参数 argsshell命令可以是字符串或者序列类型如list元组stdin, stdout, stderr分别表示程序的标准输入、输出、错误句柄preexec_fn只在Unix平台下有效用于指定一个可执行对象callable object它将在子进程运行之前被调用shell同上cwd用于设置子进程的当前目录env用于指定子进程的环境变量。如果env None子进程的环境变量将从父进程中继承。下面这2条语句执行会有什么区别 asubprocess.run(sleep 10,shellTrue,stdoutsubprocess.PIPE)
asubprocess.Popen(sleep 10,shellTrue,stdoutsubprocess.PIPE) 区别是Popen会在发起命令后立刻返回而不等命令执行结果。这样的好处是什么呢 如果你调用的命令或脚本 需要执行10分钟你的主程序不需卡在这里等10分钟可以继续往下走干别的事情每过一会通过一个什么方法来检测一下命令是否执行完成就好了。 Popen调用后会返回一个对象可以通过这个对象拿到命令执行结果或状态等该对象有以下方法 poll()Check if child process has terminated. Returns returncodewait()Wait for child process to terminate. Returns returncode attribute.terminate()终止所启动的进程Terminate the process with SIGTERMkill() 杀死所启动的进程 Kill the process with SIGKILL
send_signal(signal.xxx)发送系统信号pid 拿到所启动进程的进程号 poll asubprocess.Popen(sleep 10,shellTrue,stdoutsubprocess.PIPE)a.poll()a.poll()a.poll()
0 termiate a subprocess.Popen(for i in $(seq 1 100);do sleep 1;echo $i /tmp/sleep.log;done,shellTrue, cwd/tmp,stdoutsubprocess.PIPE)a.terminate()preexec_fn def sayhi():
... print(haha)
... a subprocess.Popen(sleep 5,shellTrue,stdoutsubprocess.PIPE,preexec_fnsayhi)a.stdout
_io.BufferedReader name3a.stdout.read()
bhaha\na.pid
2070 代码案例 import subprocess
from utils import unicode_utils
# 执行cmd或shell命令#
from multiprocessing.dummy import Pool as ThreadPooldef cmd_exec(cmd):执行shell命令返回命令返回值和结果:param cmd::return:p subprocess.Popen(cmd,shellTrue,stdinsubprocess.PIPE,stdoutsubprocess.PIPE,stderrsubprocess.PIPE)stdout, stderr p.communicate()#从PIPE中读取出PIPE中的文本,放入内存if p.returncode ! 0:return {code:p.returncode, res:unicode_utils.to_str(stderr)}return {code:p.returncode, res:unicode_utils.to_str(stdout)}if __name__ __main__:passcmd_list []cmd_list.append(df -h)cmd_list.append(ps -ef|grep jav1a|grep -v grep)cmd_list.append(ps -ef|grep pol)cmd_list.append(sh /root/ops/sum.sh)# print(cmd_exec(cmd))# Make the Pool of workerspool ThreadPool(4)# Open the urls in their own threads# and return the resultsresults pool.map(cmd_exec, cmd_list)# close the pool and wait for the work to finishprint(results)pool.close()pool.join()**************************************
unicode_utils:def to_str(bytes_or_str):把byte类型转换为str:param bytes_or_str::return:if isinstance(bytes_or_str, bytes):value bytes_or_str.decode(utf-8)else:value bytes_or_strreturn value转载于:https://www.cnblogs.com/xiao-apple36/p/8857201.html