Python-限制脚本的CPU使用率 [英] Python - Limit cpu usage for a script
问题描述
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.
感谢@Stephane指出,这实际上是我有史以来的第一个答案。
Thanks @Stephane for pointing it out, this is actually my first answer ever.
这篇关于Python-限制脚本的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!