您的位置:首页 > 编程语言 > Python开发

python subprocess 模块使用方法

2015-01-05 19:45 766 查看
Python的subprocess模块可以生成新的子进程,连接到子进程的stdin、stdout、stderr,获取子进程的返回值。这个模块的目的是替换以前的创建子进程的方法,包括:

os.system

os.spawn*

os.popen*

popen2.*

commands.*

1. Subprocess的简便用法

简便用法包括:

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)

这些方法都会阻塞,等待子进程执行完毕获取返回值。这些方法底层都使用了popen,所以本文重点演示popen的用法。

注意点: 使用这几个方法时如果使用了stdout=PIPE 或者stderr=PIPE 有可能会导致死锁。如果要使用管道,需要使用popen和communicate方法。

2. Subprocess.Popen用法

class subprocess.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)

各个参数的意义如下(参数比较多,不重要的参数字体背景为灰色)
Args 运行的命令及参数,可以是一个string,也可以是一个list

Bufsize 缓冲区大小,意义和open函数中的bufsize参数相同。0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size. A negative bufsize means to use the system default,
which usually means fully buffered. The default value for bufsize is 0 (unbuffered).

Executable 极少用到

Stdin,stdout,stderr 标准输入输出,标准错误流。Valid values are PIPE, an existing file descriptor (a positive integer), an existing file object, and None. PIPE indicates that a new pipe to the child should be created. With the default settings of None, no redirection
will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can be STDOUT, which indicates that the stderr data from the child process should be captured into the same file handle as for stdout.

preexec_fn If preexec_fn is set to a callable object, this object will be called in the child process just before the child is executed. (Unix only)

close_fds If close_fds is true, all file descriptors except 0, 1 and 2 will be closed before the child process is executed. (Unix only).

Shell 如果这个参数为true,将使用shell 执行args中的命令,而不是调用系统的execvp()函数。On Unix with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell. This means that the string must be formatted exactly
as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional
arguments to the shell itself.

Cwd If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s
path relative to cwd.

Env If env is not None, it must be a mapping that defines the environment variables for the new process; these are used instead of inheriting the current process’ environment, which is the default behavior. Note If specified, env must provide any variables
required for the program to execute.

universal_newlines If universal_newlines is True, the file objects stdout and stderr are opened as text files in universal newlines mode. Lines may be terminated by any of '\n', the Unix end-of-line convention, '\r', the old Macintosh convention or
'\r\n', the Windows convention. All of these external representations are seen as '\n' by the Python program.

Startupinfo If given, startupinfo will be a STARTUPINFO object, which is passed to the underlying CreateProcess function.

creationflags, if given, can be CREATE_NEW_CONSOLE or CREATE_NEW_PROCESS_GROUP. (Windows only)

例1:运行一个系统命令

import subprocess

try:
p = subprocess.Popen(args=['ls','/home/ding'])
except OSError, e:
print 'OSError:',e,'\n'
print 'traceback: ',e.child_traceback, '\n'
except ValueError, e:
print 'ValueError:', e, '\n'
except:
print 'unexpected exception\n'


输出如下:



说明:只传入了args参数,当args是list时,第一个元素是命令,以后的元素是命令的参数。因为没有设置stdout,所以子进程继承了父进程的stdout,这样在执行的时候命令的输出直接打印到了屏幕上。

因为默认shell为false,如果我们把ls的参数改为 ~,输出如下



即当shell为false时,不会在shell中执行命令,所以使用shell的一些特殊字符会出错。

例2:捕获子进程的输出



输出:



Communicate方法返回的是一个二元组(stdoutdata, stderrdata)

例3:进程执行超时时间

有些时候我们执行一个命令时,希望这个命令在一定时间内结束,如果超时就杀死进程 。举一个简单的例子



输出如下:



Poll方法用于判断进程是否结束,如果没有结束返回None,如果结束了 返回进程returncode。
Terminate方法在posix系统上给进程发送SIGTERM信号,该信号请求进程终止。
注意:子进程的输出保存在缓冲区中,如果输出的内容太多,缓冲区就爆了,所以官方文档对communicate方法有如下的话Note :The data read is buffered in memory, so do not use this method if the data size is large or unlimited.

附录:Popen的属性和方法
方法
Popen.poll()
Check if child process has terminated. Set and return returncode attribute.
Popen.wait()
Wait for child process to terminate. Set and return returncode attribute.
Warning
This will deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid that.
Popen.communicate(input=None)
Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if
no data should be sent to the child.
communicate() returns a tuple (stdoutdata, stderrdata).
Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.
Note

The data read is buffered in memory, so do not use this method if the data size is large or unlimited.
Popen.send_signal(signal)
Sends the signal signal to the child.
Note

On Windows, SIGTERM is an alias for terminate(). CTRL_C_EVENT and CTRL_BREAK_EVENT can be sent to processes started with acreationflags parameter which includes CREATE_NEW_PROCESS_GROUP.
New in version 2.6.
Popen.terminate()
Stop the child. On Posix OSs the method sends SIGTERM to the child. On Windows the Win32 API function TerminateProcess() is called to stop the child.
Popen.kill()
Kills the child. On Posix OSs the function sends SIGKILL to the child. On Windows kill() is an alias for terminate().
属性
警告:Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.
Popen.stdin
If the stdin argument was PIPE, this attribute is a file object that provides input to the child process. Otherwise, it is None.
Popen.stdout
If the stdout argument was PIPE, this attribute is a file object that provides output from the child process. Otherwise, it is None.
Popen.stderr
If the stderr argument was PIPE, this attribute is a file object that provides error output from the child process. Otherwise, it is None.
Popen.pid
The process ID of the child process.
Note that if you set the shell argument to True, this is the process ID of the spawned shell.
Popen.returncode
The child return code, set by poll() and wait() (and indirectly by communicate()). A None value indicates that the process hasn’t terminated yet.
A negative value -N indicates that the child was terminated by signal N (Unix only).

【参考资料】

1.https://docs.python.org/2/library/subprocess.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python