Search code examples
phpnice

How do I make php nicer to the CPU?


I'm running on a shared *NIX server (run by Site5). I have a php script that runs in the background occasionally doing some offline calculations. It uses around 100% CPU while it runs. I've tried nice-ing it, like this:

nice -n 19 php script.php

but that doesn't seem to make any difference.


Solution

  • You could scatter usleep ( int $micro_seconds ) through your code. This will force your script to stop for tiny amounts of time leaving the CPU free for other things.

    Will that be necessary though? If you script has a low priority, does it matter that it's using 100% of the CPU... If other processes with a higher priority needed the CPU wouldn't they get the time they needed and your script getting the rest (up to 100%)?