我想从 python 运行一个程序并找到它的内存使用情况。为此,我正在使用:
l=['./a.out','<','in.txt','>','out.txt']
p=subprocess.Popen(l,shell=False,stdout = subprocess.PIPE, stderr = subprocess.PIPE)
p.wait()
Res= getrusage(resource.RUSAGE_CHILDREN)
print Res.ru_maxrss
我也尝试使用check_call(l,shell=False,stdout = subprocess.PIPE, stderr = subprocess.PIPE)
并删除 p.wait 但问题是当使用 Popen 和 at 时程序卡在 p.wait() 处check_call()
当使用 check_call() 时。我无法弄清楚为什么会发生这种情况。我的论点列表是否错误?
命令./a.out < in.txt > out.txt
在终端上工作正常。我正在使用Ubuntu
有两个问题(至少):
-
<
, >
重定向由 shell 处理。subprocess
默认情况下不会生成 shell(你也不应该)
- If
stdout=PIPE
, stderr=PIPE
那么你必须从管道中读取,否则进程可能会永远阻塞
要使子进程从文件读取并写入文件:
from subprocess import check_call, STDOUT
with open('in.txt') as file, open('out.txt', 'w') as outfile:
check_call(["./a.out"], stdin=file, stdout=outfile, stderr=STDOUT)
stderr=STDOUT
合并标准输出、标准错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)