赞
踩
看python的document 自个慢慢琢磨~~,每天花一点时间学习。某某人说要厚积薄发!
先看看subprocess是干什么用的:
The subprocess modul allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.
即启用另一个线程,并可获取输入,输出以及错误信息等
先看两个函数:
subprocess.call(*popenargs, **kwargs)
Run command with arguments. Wait for command to complete, then return the returncode attribute
subprocess.check_call(*popenargs, **kwargs)
Run command with arguments. Wait for command to complete. If the exit code was zero then return, otherwise raise CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute.
>>>
>>> print subprocess.check_call(["mspaint"])
0
>>>
>>> print subprocess.call(["mspaint"])
0
我觉得两者的区别在于遇到错误的时候处理不一样,一个在返回return code ,一个会抛出异常。check_call实际上会调用call函数,然后加入了异常处理的情况。两者在本质上都会调用Popen(*popenargs, **kwargs).wait()。
>>> print subprocess.call(["mspain"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:/Python27/lib/subprocess.py", line 486, in call
return Popen(*popenargs, **kwargs).wait()
File "C:/Python27/lib/subprocess.py", line 672, in __init__
errread, errwrite)
File "C:/Python27/lib/subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>>
>>>
>>>
>>>
>>> print subprocess.check_call(["mspain"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:/Python27/lib/subprocess.py", line 499, in check_call
retcode = call(*popenargs, **kwargs)
File "C:/Python27/lib/subprocess.py", line 486, in call
return Popen(*popenargs, **kwargs).wait()
File "C:/Python27/lib/subprocess.py", line 672, in __init__
errread, errwrite)
File "C:/Python27/lib/subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>>
顺便查看了下源码:
def call(*popenargs, **kwargs):
"""Run command with arguments. Wait for command to complete, then
return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
"""
return Popen(*popenargs, **kwargs).wait()
def check_call(*popenargs, **kwargs):
"""Run command with arguments. Wait for command to complete. If
the exit code was zero then return, otherwise raise
CalledProcessError. The CalledProcessError object will have the
return code in the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
check_call(["ls", "-l"])
"""
retcode = call(*popenargs, **kwargs)
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise CalledProcessError(retcode, cmd)
return 0
关于subprocess的安全性:
不像其他的popen函数,不会直接调用/bin/sh来解释命令,也就是说,命令中的每一个字符都会被安全地传递到子进程里。
Popen是subprocess中定义的类:
Arguments are:
args should be a string, or a sequence of program arguments. The program to execute is normally the first item in the args sequence or the string if a string is given, but can be explicitly set by using the executable argument. When executable is given, the first item in the args sequence is still treated by most programs as the command name, which can then be different from the actual executable name. On Unix, it becomes the display name for the executing program in utilities such as ps .
subprocess . PIPE
Poen的方法这段属于引用,自个难得翻译了,刚好又有现成的。明白函数咋回事即可。
Popen的方法:
Popen.poll()
用于检查子进程是否已经结束。设置并返回returncode属性。
Popen.wait()
等待子进程结束。设置并返回returncode属性。
Popen.communicate(input=None)
与子进程进行交互。向stdin发送数据,或从stdout和stderr中读取数据。可选参数input指定发送到子进程的参数。 Communicate()返回一个元组:(stdoutdata, stderrdata)。注意:如果希望通过进程的stdin向其发送数据,在创建Popen对象的时候,参数stdin必须被设置为PIPE。同样,如 果希望从stdout和stderr获取数据,必须将stdout和stderr设置为PIPE。
Popen.send_signal(signal)
向子进程发送信号。
Popen.terminate()
停止(stop)子进程。在windows平台下,该方法将调用Windows API TerminateProcess()来结束子进程。
Popen.kill()
杀死子进程。
Popen.stdin
如果在创建Popen对象是,参数stdin被设置为PIPE,Popen.stdin将返回一个文件对象用于策子进程发送指令。否则返回None。
Popen.stdout
如果在创建Popen对象是,参数stdout被设置为PIPE,Popen.stdout将返回一个文件对象用于策子进程发送指令。否则返回 None。
Popen.stderr
如果在创建Popen对象是,参数stdout被设置为PIPE,Popen.stdout将返回一个文件对象用于策子进程发送指令。否则返回 None。
Popen.pid
获取子进程的进程ID。
Popen.returncode
获取进程的返回值。如果进程还没有结束,返回None。
关于一些替换:
==>
output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
output=`mycmd myarg`
output = p2.communicate()[0]
sts = os.system("mycmd" + " myarg")
==>
p = Popen("mycmd" + " myarg", shell=True)
sts = os.waitpid(p.pid, 0)[1]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。