我是靠谱客的博主 拼搏蛋挞,最近开发中收集的这篇文章主要介绍php sleep usleep,PHP: usleep - Manual,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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();

最后

以上就是拼搏蛋挞为你收集整理的php sleep usleep,PHP: usleep - Manual的全部内容,希望文章能够帮你解决php sleep usleep,PHP: usleep - Manual所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部