php写的命令行服务,有时候在不需要执行的时候,需要尽可能的降低消耗资源,最简单的降低消耗就是sleep了。 单次循环sleep间隔时间,有时候不满足需求,当前的sleeper采用了类似tcp的重传定时器, 不过,这个东西越sleep越贪睡奥,需要适度控制。 ?phpabstra
php写的命令行服务,有时候在不需要执行的时候,需要尽可能的降低消耗资源,最简单的降低消耗就是sleep了。
单次循环sleep间隔时间,有时候不满足需求,当前的sleeper采用了类似tcp的重传定时器,
不过,这个东西越sleep越贪睡奥,需要适度控制。
<?php
abstract class sleeper {
protected $init_sleep_time;
protected $sleep_time = 0;
public function sleeper($init_sleep_time){
$this->sleep_time = $init_sleep_time;
$this->init_sleep_time = $init_sleep_time;
}
public function real_sleep(){
if($this->need_sleep()){
echo $this->sleep_time . "\n";
sleep($this->sleep_time);
$this->sleep_time += 3;
} else {
$this->sleep_time = $this->init_sleep_time;
}
}
public abstract function need_sleep();
}
class test_sleeper extends sleeper {
public function sleeper($init_sleep_time = 1){
$this->sleep_time = $init_sleep_time;
}
public function need_sleep(){
return true;
}
}
$sl = new test_sleeper(1);
for($i = 0; $i< 10; $i++){
$sl->real_sleep();
}
原文地址:一个贪睡的sleeper, 感谢原作者分享。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号