给定一个非常简单的脚本,例如:
import subprocess
while True:
x = subprocess.Popen('ls -ltr /usr/lib', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
我在笔记本电脑的 Ubuntu 12.04(32 位)celeron 2 核上得到以下“顶级”cpu 使用情况:
x = subprocess.Popen('ls -l /usr/lib', ...
python3.2.3: 6%, python2.7.3: 5% (ls 0%)
x = subprocess.Popen('gcc --version', ...
python3.2.3: 22%, python2.7.3: 18% (gcc 0%)
x = subprocess.Popen('pwd', ...
python3.2.3: 47%, python 2.7.3: 35% (pwd 0%)
如果将 subprocess.Popen 替换为 os.popen,则结果不同:
x = os.popen('ls /usr/lib').read()
python3.2.3: 8%, python2.7.3: 3% (gcc 0%)
x = os.popen('gcc --version').read()
python3.2.3: 45%, python 2.7.3: 9%
x = os.popen('pwd').read()
python3.2.3: 68%, python 2.7.3: 22%
为什么 CPU 使用率如此之低且如此不同?我在core i5和atom ubuntus上试过这个,结果接近100%(3.2)和80%(2.7)。上面没有其他内容,我的平台有什么问题?