Python Popen communicate() 和wait()的区别体验

测试环境centos7
ulimit -a 看到的 pipe size 是 4KB,那只是每页的大小,查询得知 linux 默认的 pipe size 是 64KB
首先看官方文档解释二者的区别
Popen.wait()
Wait for child process to terminate. Set and return returncode attribute.(等待子进程终止。设置并返回returncode属性。)
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.(使用stdout = PIPE和/或stderr = PIPE时,这将导致死锁,并且子进程会向管道生成足够的输出,从而阻塞等待OS管道缓冲区接受更多数据的等待。使用communication()可以避免这种情况。)

简单说communicate() 和wait()的区别,使用 subprocess 模块的 Popen 调用外部程序,如果 stdout 或 stderr 参数是 pipe,并且程序输出超过操作系统的 pipe size时,如果使用 Popen.wait() 方式等待程序结束获取返回值,会导致死锁,程序卡在 wait() 调用上。看个例子
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.(请注意,如果要将数据发送到流程的stdin,则需要使用stdin = PIPE创建Popen对象。同样,要在结果元组中获得除None以外的任何内容,您还需要提供stdout = PIPE和/或stderr = PIPE。)

Note:
The data read is buffered in memory, so do not use this method if the data size is large or unlimited.(读取的数据缓存在内存中,因此,如果数据大小很大或没有限制,请不要使用此方法。)

import subprocess


def test(size):
    print 'start'
    cmd = 'dd if=/dev/zero bs=1 count={} 2>/dev/null'.format(size)
    p = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
    p.wait()
    # p.communicate()#它也可以设置超时的时间timeout=60,超时60秒
    print 'end'


# 64KB
print 'size==64K'
test(64 * 1024)

# 64KB + 1B
print('size>64K')
test(64 * 1024 + 1)

使用p.wait()时的运行结果为,卡死:
在这里插入图片描述
使用p.communicate()时的运行结果为,正常结束:
在这里插入图片描述
官方文档里推荐使用 Popen.communicate()。这个方法会把输出放在内存,而不是管道里,所以这时候上限就和内存大小有关了,一般不会有问题。而且如果要获得程序返回值,可以在调用 Popen.communicate() 之后取 Popen.returncode 的值。

结论:如果使用 subprocess.Popen,就不使用 Popen.wait(),而使用 Popen.communicate() 来等待外部程序执行结束。

猜你喜欢

转载自blog.csdn.net/mingtiannihaoabc/article/details/103489637