note 10332 added to usleep
| From: | dave at techweavers dot net | Date: | Thu, 28 Dec 2000 21:33:43 +0000 |
| Subject: | note 10332 added to usleep | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-9159@lists.php.net to get a copy of this message | ||
To monitor a scripts CPU ussage and avoid any nasty CPU gobbling loops you can use this function
(will not work with windows or safe mode) I know it works on FreeBSD
function phpmon($max)
{
$usage = $max +1;
$sleeprate = 500;
$loops = 1;
while ($usage >= $max)
{
$cmd =
ps -Unobody -r -o%cpu;
$lines = explode("\n", $cmd);
$usage = substr($lines[1], 0, strpos($lines[1], "."));
$sleeprate = $loops + $sleeprate;
usleep($sleeprate);
$loops+=100;
}
}
just place this in your potential problem loop:
phpmon($MAX);
where $MAX is the maximum CPU you want the process to consume. e-mail me with any
improvements/suggestions.
http://www.php.net/manual/function.usleep.php