【发布时间】:2018-11-05 20:25:18
【问题描述】:
我有一个 Linux 命令列表,我正在使用 subprocess 模块运行每个命令,我需要弄清楚如何找到每个命令的执行时间并在列表或字典中返回执行时间,在dict 的情况下,Key 可以是命令名称,值可以是时间,以秒为单位。
即(Popen object).poll() == 0 结束的那一刻应该决定结束时间。
我正在使用 Python 3.5.2
例如:
import time
import shlex
from subprocess import *`
#list of commands
commands = ['sleep 5', 'ls -l', 'find /usr','sleep 3','uptime']
#executing each command in the list
for command in commands:
cmd = shlex.split(command)
# Need to time this and the moment it is finished executing.
x = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
应该返回/打印:
- Commands Time(secs)
- sleep 5 5.0004 #whatever the time it takes
- ls -l 0.000 #and so on....
【问题讨论】:
-
你需要命令的输出吗?
-
@Jean-FrançoisFabre 我不知道,所以我不在乎我是否得到输出。
标签: python linux python-3.x time subprocess