获取python子进程花费了多少时间 [英] get how much time python subprocess spends

查看:48
本文介绍了获取python子进程花费了多少时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算子流程需要多长时间.我尝试使用

I'd like to time how long does the subprocess take. I tried to use

start = time.time()
subprocess.call('....')
elapsed = (time.time() - start)

但是它不是很准确(不确定与多进程或其他什么有关)有没有更好的方法可以获得子流程真正花费的时间?

However it's not very accurate (not sure related to multi-process or sth else) Is there a better way I can get how much time the subprocess really spends?

谢谢!

推荐答案

这取决于你想要的时间;已用时间、用户模式、系统模式?

It depends on which time you want; elapsed time, user mode, system mode?

使用 resource.getrusage,您可以查询当前进程的子进程的用户模式和系统模式时间.这仅适用于 UNIX 平台(例如 Linux、BSD 和 OS X):

With resource.getrusage you can query the user mode and system mode time of the current process's children. This only works on UNIX platforms (like e.g. Linux, BSD and OS X):

import resource
info = resource.getrusage(resource.RUSAGE_CHILDREN)

在 Windows 上,您可能必须使用 ctypes 来获取来自 WIN32 API 的等效信息.

On Windows you'll probably have to use ctypes to get equivalent information from the WIN32 API.

这篇关于获取python子进程花费了多少时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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