Reputation: 873
I have an application written in PHP (Not website, an application), that has an infinite loop for its logic until the application shuts down. One issue with it that I've noticed is I get incredibly high CPU usage (Obviously), and I've tried using C++ methods I've done in the past to lower the CPU usage. Not many of them seem to work.
I've tried:
sleep(1);
and:
time_sleep_until(microtime(true)+0.2);
But neither seem to lower utilization.
Any tips?
Upvotes: 1
Views: 1148
Reputation: 1249
You can configure execution of your jobs when they are needed to be executed. So if this infinite script needs to do/check something every minute you can just do it when there is a change otherwise sleep.
Your problem can be on something else your script does other than itself. Here is an example hope it helps.
set_time_limit(0);
ignore_user_abort(true);
$minutes = NULL;
do {
try {
// Run jobs, only when the minute has changed
$now = getdate();
if ($now['minutes'] != $minutes) {
$minutes = $now['minutes'];
}
}
catch (Exception $e) {
// Catch here and handle
}
sleep(10);
} while (TRUE);
Upvotes: 1
Reputation: 15374
As far as I know there is no way to do this within the PHP application, but when launching the process you can pass it to a command-line utility called cpulimit
. Another option is to change the nice
level of the program so at least when it is busy it won't hold up other more important processes.
Upvotes: 0