python 2.7和3.3子进程模块区别

标签 python python-2.7 python-3.x subprocess python-3.3

我有以下代码

from __future__ import print_function
import subprocess

p = subprocess.Popen(['cat'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.kill()
p.wait()
print(p.stdin.write(b'some'))

我用 python 2.7 和 python 3.3 运行它。在 python 2.7 中,当我尝试写入 stdin 时会抛出异常。对于 python 3.3,这段代码打印 4。python 2.7 子进程模块和 python 3.3 有什么区别?

最佳答案

区别在于 bufsize 的默认值。它在 Python 2.7 中为 0(无缓冲),因此 write 导致 EPIPE 错误。管道在 Python 3.2+ 中完全缓冲,即,在您刷新缓冲区之前,不会检测到错误。来自 subprocess' docs :

Changed in version 3.3.1: bufsize now defaults to -1 to enable buffering by default to match the behavior that most code expects. In versions prior to Python 3.2.4 and 3.3.1 it incorrectly defaulted to 0 which was unbuffered and allowed short reads. This was unintentional and did not match the behavior of Python 2 as most code expected.

关于python 2.7和3.3子进程模块区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20565026/

相关文章:

python-2.7 - Pyspark 应用程序仅部分利用 dataproc 集群资源

python - 在纪元中转换时出现“未知”时间戳格式

python - 了解递归调用中的 Python 列表内存使用情况

python - 将一个点捕捉到一条线的最近部分?

python-2.7 - Python-网络x : Neighbours with certain weight

python - 在 slim python 3.6 docker 镜像上运行 python mysql 客户端

Python 多处理跳过子段错误

python - 导入 nltk 无法理解的错误

python - 测试套件的 Pytest 排序

python - Tensorflow-GPU 不使用带有 CUDA、CUDNN 的 GPU