6

It's not the first time I get a too much cpu load warning from my hosting. The code is just some random php script with mysql queries, nothing fancy. (THe tables are nothing extraordinary, a few hundred lines maximum and I always limit them if requested.

I don't mind if it runs for 0.15 second instead of 0.05, so is there a way I can control process priority or limit cpu load?

Thanks!

3 Answers 3

12

If this is a dameon or program that runs for long time add sleep()/usleep(). A small sleep will reduce your CPU usage dramatically.

Following code will consume a lot of cpu

while(...){
//do stuff
}

Because you are not giving room for CPU to do other task here. change it to

while(...){
   //do stuff
    sleep(1);
}

This will greatly decrease your CPU usage. 1 second for CPU is a lot of time to do other task.

To sleep that extra 0.1 second (0.15 - 0.05) use usleep().

usleep(100000);
2
  • Acually, it will not reduce usage, as it will remain the same. But will pervent process from spiking to 100% CPU usage for whole duration of a script. Which will not block other tasks that should be executed faster.
    – Grzegorz
    Commented Jun 26, 2015 at 9:16
  • So smart and so useful. Thank you very much. Just what I needed Commented Mar 3, 2021 at 16:43
6

In principle, on Unixish system (Linux, BSD, etc.), you could use the proc_nice() function to change the process priority, like this:

proc_nice( 20 );  // now this process has very low priority

However, there are a couple of major caveats here that make it less useful in practice:

  • Until PHP 7.2.0 it wasn't supported on Windows at all.
  • You're only allowed to increase the niceness, not to decrease it (not even back to its original value).
  • The niceness persists until the PHP process exits, which could be a problem if you're running PHP as a FastCGI process, or, even worse, as a webserver extension.
  • Because of the above issues, proc_nice() might be disabled for security reasons even on systems that could technically support it.

What you could try to do, if your webhost allows it, is to start a background process for the long-running task, so that your webserver can get back to serving requests while it's running. You can even use the nice shell command to lower the priority of the background process, like this:

exec( "nice nohup php -f slow_script.php < /dev/null > output.txt 2>&1 &" );

Once the slow script has finished, you can get its output by downloading output.txt.

5
  • Thank you for your kind answer! I'd mark this as useful, but I don't have enough rep yet :)
    – molbal
    Commented Jan 9, 2013 at 23:23
  • Actually, my first suggestion wasn't that useful, as I noted, but I just added a possibly more useful one -- see above. :) Commented Jan 9, 2013 at 23:27
  • Thanks again! I didn'T want to hassle with this, because I work mostly from home where I must run Windows. On the other hand, I have saved these snippets. I didn't know PHP is capable of this, I learn something new every day :)
    – molbal
    Commented Jan 9, 2013 at 23:44
  • @Ilmari Karonen see custom_proc_nice() function from the documentation to deal with caveat 2 and 3.
    – RafaSashi
    Commented Mar 30, 2015 at 14:13
  • proc_nice only works with Windows. See: php.net/manual/en/function.proc-nice.php Commented Mar 3, 2021 at 16:41
0

In addition to Ilmari Karonen's answer:

If you want to increase or decrease the niceness of the current process use renice together with the current process id:

function custom_proc_nice($priority) {

  exec("renice +$priority ".getmypid());

}

If you want to prevent the niceness to persist after the PHP process exits you will need a shutdown function:

function exit_func(){

  // Restore priority
  proc_nice(0);
}

register_shutdown_function('exit_func');

Resources

http://php.net/manual/en/function.proc-nice.php

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.