之间的什么区别使PIPE对于前者的安全性降低了

之间的什么区别使PIPE对于前者的安全性降低了

本文介绍了subprocess.call()和subprocess.Popen()之间的什么区别使PIPE对于前者的安全性降低了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了两个文档.

I've had a look at the documentation for both of them.

此问题由JF的评论提示:检索以下内容的输出subprocess.call()

This question is prompted by J.F.'s comment here: Retrieving the output of subprocess.call()

subprocess.call() 的当前Python文档说以下内容关于将PIPE用于subprocess.call():

Python 2.7 subprocess.call() :

Python 2.7 subprocess.call():

Python 2.6不包含此类警告.

Python 2.6 includes no such warnings.

此外, subprocess.call()subprocess.check_call() 除了将stdout = PIPE与communication()结合使用外,似乎没有其他方法可以访问其输出:

Also, the subprocess.call() and subprocess.check_call() don't seem to have a way to access their output, except for using stdout=PIPE with communicate():

https://docs.python.org/2.6/library /subprocess.html#convenience-functions

https://docs.python.org/2.6 /library/subprocess.html#subprocess.Popen.communicate

subprocess.call()subprocess.Popen()之间的区别是什么使得PIPE对于subprocess.call()的安全性降低?

What difference between subprocess.call() and subprocess.Popen() makes PIPE less secure for subprocess.call()?

更具体:为什么subprocess.call() 死锁基于子进程的输出量." 而不是Popen()?

More Specific: Why does subprocess.call() "deadlock based on the child process output volume.", and not Popen()?

推荐答案

call()就是Popen().wait()(±错误处理).

call() is just Popen().wait() (± error handling).

您不应将stdout=PIPEcall() 一起使用,因为它不会从管道中读取,因此子进程将在填充相应的OS管道缓冲区后立即挂起.这是一张显示数据如何在command1 | command2 Shell管道中流动的图片:

You should not use stdout=PIPE with call() because it does not read from the pipe and therefore the child process will hang as soon as it fills the corresponding OS pipe buffer. Here's a picture that shows how data flows in command1 | command2 shell pipeline:

Python版本是什么都没关系-管道缓冲区(看图片)在Python进程之外. Python 3不使用C stdio,但仅影响内部缓冲.刷新内部缓冲区后,数据将进入管道.如果command2(您的父Python程序)没有从管道读取,则command1(例如由call()启动的子进程)将在管道缓冲区已满( pipe_size = fcntl(p.stdout, F_GETPIPE_SZ) 〜65K(最大值为/proc/sys/fs/pipe-max-size〜1M))

It does not matter what your Python version is -- the pipe buffer (look at the picture) is outside of your Python process. Python 3 does not use C stdio but it affects only the internal buffering. When the internal buffer is flushed the data goes into the pipe. If command2 (your parent Python program) does not read from the pipe then command1 (the child process e.g., started by call()) will hang as soon as the pipe buffer is full (pipe_size = fcntl(p.stdout, F_GETPIPE_SZ) ~65K on my Linux box (max value is /proc/sys/fs/pipe-max-size ~1M)).

如果以后要从管道中读取内容,则可以使用stdout=PIPE ,例如,使用Popen.communicate()方法.您还可以直接从process.stdout(代表管道的文件对象)中读取.

You may use stdout=PIPE if you read from the pipe later e.g., using Popen.communicate() method. You could also read from process.stdout (the file object that represents the pipe) directly.

这篇关于subprocess.call()和subprocess.Popen()之间的什么区别使PIPE对于前者的安全性降低了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 21:32