Home > php教程 > php手册 > 一个贪睡的sleeper

一个贪睡的sleeper

WBOY
Release: 2016-06-06 20:14:22
Original
1063 people have browsed it

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; $ireal_sleep();
}

Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template