note 68433 added to function.usleep
| From: | Patrick at osu1 dot php dot net | Date: | Wed, 26 Jul 2006 20:04:34 +0000 |
| Subject: | note 68433 added to function.usleep | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115230@lists.php.net to get a copy of this message | ||
I want to create a daemon/Linux service. Here is an example of how to run a process that has
"throttle control"
// You must set these
//
// max_execution_time = 0
// max_input_time = 0
function doProcess() {
echo "Start"."\n";
usleep(10000);
echo "Stop"."\n";
return false;
}
function manageProcess() {
// Setup data
$runsPerMinute = 200;
$maxMinuteAverage = 5;
$waitIfNotWorking = 120; // seconds
// Conversion
$microsPerSecond = 1000000;
// Statistical Info
$currentMinute = 0;
$minute = -1;
$countPerMinute = array();
$sumPerMinute = array();
// Totals
$totalProcessTime = 0;
$totalCounts = 0;
while (true) {
$timestart = microtime();
$performedWork = doProcess();
$timeend = microtime();
if (!$performedWork) {
// Statistical Info
$currentMinute = 0;
$minute = -1;
$countPerMinute = array();
$sumPerMinute = array();
sleep($waitIfNotWorking);
} else {
$ts = split(" ",$timestart);
$te = split(" ",$timeend);
$te[0] = ($te[0] * $microsPerSecond) - ($ts[0] * $microsPerSecond);
$te[1] = ($te[1] - $ts[1]) * $microsPerSecond;
$processTime = $te[0] + $te[1];
if (date("i")<>$minute) { // We are NOT in the same minute
// Reset the new minute
$minute = date("i");
$currentMinute = ($currentMinute+1) % $maxMinuteAverage;
// Remove Statistical Information from the minute we are expiring.
if (isset($countPerMinute[$currentMinute])) {
$totalProcessTime = $totalProcessTime - $sumPerMinute[$currentMinute];
$totalCounts = $totalCounts - $countPerMinute[$currentMinute];
}
$countPerMinute[$currentMinute] = 0;
$sumPerMinute[$currentMinute] = 0;
}
$countPerMinute[$currentMinute] = $countPerMinute[$currentMinute] + 1;
$sumPerMinute[$currentMinute] = $sumPerMinute[$currentMinute] + $processTime;
$totalCounts = $totalCounts + 1;
$totalProcessTime = $totalProcessTime + $processTime;
$averageRuntime = round($totalProcessTime / $totalCounts);
$waitTime = (($microsPerSecond*60) / $runsPerMinute) - $averageRuntime;
usleep($waitTime);
}
}
}
manageProcess();
----
Server IP: 216.194.113.175
Probable Submitter: 155.212.59.41
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.usleep.php
Edit -- http://master.php.net/note/edit/68433
Del: integrated -- http://master.php.net/note/delete/68433/integrated
Del: useless -- http://master.php.net/note/delete/68433/useless
Del: bad code -- http://master.php.net/note/delete/68433/bad+code
Del: spam -- http://master.php.net/note/delete/68433/spam
Del: non-english -- http://master.php.net/note/delete/68433/non-english
Del: in docs -- http://master.php.net/note/delete/68433/in+docs
Del: other reasons-- http://master.php.net/note/delete/68433
Reject -- http://master.php.net/note/reject/68433
Search -- http://master.php.net/manage/user-notes.php