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!

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
molbal
  • 974
  • 1
  • 10
  • 23

3 Answers3

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);
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • 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 Jun 26 '15 at 09:16
  • So smart and so useful. Thank you very much. Just what I needed – Juergen Schulze Mar 03 '21 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.

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • Thank you for your kind answer! I'd mark this as useful, but I don't have enough rep yet :) – molbal Jan 09 '13 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. :) – Ilmari Karonen Jan 09 '13 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 Jan 09 '13 at 23:44
  • @Ilmari Karonen see custom_proc_nice() function from the documentation to deal with caveat 2 and 3. – RafaSashi Mar 30 '15 at 14:13
  • proc_nice only works with Windows. See: https://www.php.net/manual/en/function.proc-nice.php – Juergen Schulze Mar 03 '21 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

RafaSashi
  • 16,483
  • 8
  • 84
  • 94
  • Alas, unless you're running PHP as the root user (which is terribly unsafe, so you really shouldn't be), [`renice` cannot decrease the niceness of a process, either.](http://linux.die.net/man/8/renice) – Ilmari Karonen Mar 30 '15 at 14:23
  • Good to know. Thanks for the information and reactivity Ilmari ! Is the default value for a php process set to 0? – RafaSashi Mar 30 '15 at 14:30