subprocess.call()和subprocess.Popen()之间的什么区别使PIPE对于前者的安全性降低了? [英] What difference between subprocess.call() and subprocess.Popen() makes PIPE less secure for the former?

查看:423
本文介绍了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():

注意请勿将此功能与stdout=PIPEstderr=PIPE一起使用.如果子进程为管道生成足够的输出以填充OS管道缓冲区,则该子进程将阻塞,因为未从中读取管道.

Note Do not use stdout=PIPE or stderr=PIPE with this function. The child process will block if it generates enough output to a pipe to fill up the OS pipe buffer as the pipes are not being read from.

Python 2.7 subprocess.call() :

Python 2.7 subprocess.call():

注意请勿将此功能与stdout=PIPEstderr=PIPE一起使用,因为这可能会基于子进程的输出量而死锁.需要管道时,可以将Popen与communication()方法一起使用.

Note Do not use stdout=PIPE or stderr=PIPE with this function as that can deadlock based on the child process output volume. Use Popen with the communicate() method when you need pipes.

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

请注意,如果要将数据发送到进程的stdin,则需要使用stdin=PIPE创建Popen对象.同样,要在结果元组中获取除None以外的任何内容,您也需要提供stdout=PIPE和/或stderr=PIPE.

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.

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() 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()启动的子进程)将在管道缓冲区已满(

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对于前者的安全性降低了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆