美文网首页
subprocess

subprocess

作者: shuff1e | 来源:发表于2018-03-20 22:09 被阅读24次

subprocess.PIPE

在创建Popen对象时,subprocess.PIPE可以初始化stdin, stdout或stderr参数。表示与子进程通信的标准流。

subprocess.STDOUT

创建Popen对象时,用于初始化stderr参数,表示将错误通过标准输出流输出。

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。

  • 使用subprocess.communicate()方法
[root@shuffle-dev cloudsvc-nosql-ops]$ vim subp.py
#!/usr/bin/env python                                                                                                                         
# -*- coding: utf8 -*-

import subprocess

def _executeCmd(cmd):
    try:
        p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        print 'poll:',p.poll()
        std,stderr=p.communicate()
        print 'std',std,'stderr:',stderr
        print 'returncode:',p.returncode
        #下面的output和retval没有结果    
        output=""
        for line in p.stdout:
            output=output+line
        retval=p.wait()
        print 'retval:',retval
        print 'output:',output
        return output
    except BaseException,e:
        pass
if __name__ == '__main__':
    cmd='ls -l /root'
    _executeCmd(cmd)
[root@shuffle-dev cloudsvc-nosql-ops]$ ./subp.py  
poll: None
std total 2104
drwxr-xr-x 5 root root    4096 Jan 29 21:07 go.test
drwxr-xr-x 5 root root    4096 Feb  3 20:28 LCLSSBed3
drwxr-xr-x 6 root root    4096 Feb 28 17:59 others
drwxr-xr-x 2 root root    4096 Mar  3 19:55 py_test
drwxr-xr-x 2 root root    4096 Jan 31 17:37 shuffle_tools
stderr: None
returncode: 0
  • 使用stdout
[root@shuffle-dev cloudsvc-nosql-ops]$ vim subp.py
#!/usr/bin/env python
# -*- coding: utf8 -*-

import subprocess

def _executeCmd(cmd):
    try:
        p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        print 'poll:',p.poll()
        #std,stderr=p.communicate()
        #print 'std',std,'stderr:',stderr
        #print 'returncode:',p.returncode                                                                                                     
        output=""
        for line in p.stdout:
            output=output+line
        retval=p.wait()
        print 'retval:',retval
        print 'output:',output
        return output
    except BaseException,e:
        pass
if __name__ == '__main__':
    cmd='ls -l /root'
    _executeCmd(cmd)
poll: None
retval: 0
output: total 2104
drwxr-xr-x 5 root root    4096 Jan 29 21:07 go.test
drwxr-xr-x 5 root root    4096 Feb  3 20:28 LCLSSBed3
drwxr-xr-x 6 root root    4096 Feb 28 17:59 others
drwxr-xr-x 2 root root    4096 Mar  3 19:55 py_test
drwxr-xr-x 2 root root    4096 Jan 31 17:37 shuffle_tools

相关文章

网友评论

      本文标题:subprocess

      本文链接:https://www.haomeiwen.com/subject/cupxfftx.html