Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make php nicer to the CPU?

Tags:

php

nice

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.

like image 820
Ben Avatar asked Dec 05 '25 06:12

Ben


2 Answers

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%)?

like image 70
Stacey Richards Avatar answered Dec 07 '25 21:12

Stacey Richards


Even niced, it'll use 100% CPU if available. However, the kernel will give priority to any other (non-niced) processes that come along.

like image 41
Brian Knoblauch Avatar answered Dec 07 '25 21:12

Brian Knoblauch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!