describes how

has recently deployed crontab in the company, whether the sudden fantasy can use PHP to implement a timer, granularity to second level, because crontab is up to the minute level, and it also investigates a few timers implemented in PHP. Swoole extends to a millisecond timer that is very efficient, but after all, it is not written in pure PHP code, so finally it is considered to use PHP to implement a timer class for learning reference. When the

implements the

to implement the timer code, it uses the two extended

Pcntl - multi process extensions that the PHP system brings: it is mainly to allow a number of processes to be opened at the same time, and to handle some tasks in parallel.

Spl - SplMinHeap - small top stack

a small top stack data structure, when implementing a timer, it is good to use this structure efficiency. The time complexity of inserting and deleting is O (logN), and a timer like libevent is also used before the 1.4 version of this data structure is rbtree, If you use a linked list or a fixed array, each insertion or deletion may need to be re traversed or sorted. There are still some performance problems.


2, and then all registered into our timer class Timer.
3, call the timer class monitor method, start to listen to the.
4, the monitoring process is a while dead cycle, constantly to see whether the pile top of the time heap is due, originally considered once a second cycle, after watching once a second cycle, after. It's a bit of a problem to think about it once a second. If the timer is due at our sleep (1), we can't do it exactly right away. There may be a risk of delay, so we still use usleep (1000) milliseconds to see and also hang up the process to reduce CPU load.


 / * * * * Class Timer * / class Timer extends {/ * * * / * * comparison root node and new insert node size ($value2) {if ($value1['timeout'] > $value2['timeout']) {return -1;} if ($value1['timeout'] < $value2['timeout']) {return 1;} 0;} / * * insert node * / {} {} Ue['timeout'] = time () + $value['expire']; parent:: insert ($value);} / * * / * monitor * @param bool $debug * / public function monitor ({] {} {}; {} (1000);}} / * * execution * Param $debug * / private function exec ($debug) {$hit = 0; $t1 = microtime (true); while (! $this->)) CT ()): $this-> extract (); $hit = 1; / / / / open sub process if (pcntl_fork () = = = 0) {empty ($node['action'])? '': call_user_func ($node['action']); exit (0);} / / / ignore subprocesses, subprocesses withdraw from system recycle pcntl_signal D, SIG_IGN);} else {break;}} $t2 = microtime (true); echo ($debug & & $hit)? 'time heap adjustment time:'. Timer (); / / / / registration - 3S - trigger $timer-> insert (array ('expire'=>'repeat' => true,'action'=>); Echo'3 sec - repeat - gogo'. "rn";})); / / / registration - 6S - triggering a $timer-> insert (array ('expire' =>'repeat'=>); N: center "> 

also tested more extreme cases, while the 1000 timer 1s all expired, the time heap all adjusted only 0.126s this is no problem, but each time a timer is needed to adjust. To open a sub process, this may be time-consuming, and it is possible that the 1s will not finish these 1000, which will affect the next monitor to continue to trigger, but not to open the subprocess, such as the direct execution should be finished. Of course, there must be a better way.

summary of

above is a small compilation of the PHP multitask second level timer implementation method, hope to help you, if you have any questions welcome to me message, the small editor will respond to everyone in time!

you may be interested in:

This concludes the body part

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:The implementation of PHP multitask second level timer | Script Home

You may also be interested in these articles!