Can I throttle the max CPU usage of a php script?

后端 未结 3 2187
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-15 23:54

I have some scripts that use a ton of cpu is it possible to cap the amount of cpu a process is allowed to use? I am running on CentOs 5.5 by the way.

3条回答
  •  既然无缘
    2021-02-16 00:26

    PHP is considered a scripting language, and does not have such low level access to the hardware.

    Instead, what you can do is use functions like "set_time_limit()"

    http://php.net/manual/en/function.set-time-limit.php

    and memory_limit in your php.ini

    http://php.net/manual/en/ini.core.php

    Those are the recommended methods, but the closest you'll get to what you want are probably a combination of "sleep()"

    http://php.net/manual/en/function.sleep.php

    and getting the current CPU load with "exec('uptime');". Note that you may or may not have access to those system commands.

提交回复
热议问题