Python-限制脚本的CPU使用率 [英] Python - Limit cpu usage for a script

查看:573
本文介绍了Python-限制脚本的CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python 脚本是否有可能限制分配给它的 cpu功能

现在,我有一个脚本(仅使用个内核)正在使用一个CPU内核的 100% 。我需要它使用的金额少于该金额。

Is it possible for a python script to limit the cpu power allocated to it.
Right now, I have a script (using only one core) that is using 100% of one cpu's core. I need it to use less than that amount.

我不知道是否有等同于php的 memory_limit 在python中?还是是否有一个库来管理内存分配(使用 sleep 不是一个非常优雅的解决方案吗?

I don't know if there is an equivalent to php's memory_limit in python? or if is there a library to manage memory allocation (using sleep isnt a very elegant solution?

谢谢。 / p>

Thanks.

推荐答案

也许尝试使用Python的 资源包?

Maybe try Python's "resource" package ?

资源包基本上是UNIX中 setrlimit的包装。因此,您可以为CPU /内存使用率等。

The resource package is basically a wraparound of "setrlimit" in UNIX. So you can set constrains for CPU/Memory usage, etc.

更多关于UNIX中的 setrlimit

感谢@Stephane指出,这实际上是我有史以来的第一个答案。

Thanks @Stephane for pointing it out, this is actually my first answer ever.

这篇关于Python-限制脚本的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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