简体   繁体   English

Python-限制脚本的CPU使用率

[英]Python - Limit cpu usage for a script

Is it possible for a python script to limit the cpu power allocated to it. python脚本是否有可能限制分配给它的cpu power
Right now, I have a script (using only one core ) that is using 100% of one cpu's core. 现在,我有一个脚本(仅使用one core )正在使用一个cpu内核的100% I need it to use less than that amount. 我需要使用少于该数量的东西。

I don't know if there is an equivalent to php's memory_limit in python? 我不知道在python中是否有等同于php的memory_limit or if is there a library to manage memory allocation (using sleep isnt a very elegant solution? 或者是否有一个库来管理内存分配(使用sleep不是一个非常优雅的解决方案?

Thanks. 谢谢。

Maybe try Python's " resource " package ? 也许尝试使用Python的“ 资源 ”包?

The resource package is basically a wraparound of "setrlimit" in UNIX. 该资源包基本上是UNIX中“ setrlimit”的包装。 So you can set constrains for CPU/Memory usage, etc. 因此,您可以设置CPU /内存使用率的限制等。

More about "setrlimit" in UNIX 有关UNIX中的“ setrlimit”的更多信息

Thanks @Stephane for pointing it out, this is actually my first answer ever. 感谢@Stephane指出,这实际上是我有史以来的第一个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM