Workerman Timer use

Source: Internet
Author: User
Tags autoloader

From:http://doc3.workerman.net/worker-development/add.html

Add
$time_interval, callable $callback [,$args = array(), bool $persistent = true])

Perform a function or class method on a timed basis

Parameters

time_interval

How long it takes to execute, in seconds, with decimals, and accurate to 0.001, that is, to the millisecond level.

callback

callback function注意:如果回调函数是类的方法,则方法必须是public属性

args

The parameters of the callback function must be an array, the element of which is the parameter value

persistent

If it is persistent, pass false if you want to execute it only once (the task that executes only once will be automatically destroyed after execution, not necessarily called Timer::del() ). The default is true, which is always performed on a timed basis.

return value

Returns an integer representing the timerid of the timer, which can be destroyed by a call Timer::del($timerid) .

Example 1, the Timer function is an anonymous function (closure)
Use \Workerman\Worker;Use \Workerman\Lib\Timer;Require_once $task = new Worker ();  $task->count = 1; $task->onworkerstart = function ( $task) {//executes  $time _interval = 2.5; Timer::add ( $time _interval,  Function () {echo  "task run\n";}); //run Workerworker::runall ();          
2, the timing function is a common function
Require_once'./workerman/autoloader.php ';Use \Workerman\Worker;Use \Workerman\Lib\Timer;Common functionsfunctionSend_mail($to,$content) {echo  "send mail ... \ n";}  $task = new Worker (); function  ( $task) { $to = " [email protected] ';  $content =  ' hello Workerman '; //10 seconds after the Send Mail task, the last parameter passed false, indicating that only run once Timer::add (10,  ' Send_mail ', array ( $to,  $content), false);}; //run Workerworker::runall ();          
3. The method of timing function as Class
Require_once'./workerman/autoloader.php ';Use \Workerman\Worker;Use \Workerman\Lib\Timer;Classmail{Note that the callback function property must be publicPublicfunctionSend($to,$content) {Echo"Send mail ... \ n"; }}$task =new Worker ();  $task->onworkerstart =  Function ( $task) {//10 seconds after sending a message Span class= "hljs-variable" > $mail = new Mail ();  $to =  ' [email protected] ';  $content =  ' hello Workerman '; Timer::add (10, array ( $mail, Span class= "hljs-string" > ' send '), array ( $to,  $content), false);}; //run Workerworker::runall ();           
4. The timer function is the class method (the timer is used inside the class)
Require_once'./workerman/autoloader.php ';Use \Workerman\Worker;Use \Workerman\Lib\Timer;Classmail{Note that the callback function property must be publicPublicfunctionSend($to,$content) {Echo"Send mail ... \ n"; }PublicfunctionSendlater($to,$content) {The callback method belongs to the current class, then the first element of the callback array is $this Timer::add (10,Array$this,' Send '),Array $to,  $content), false);}  $task = new Worker (); function  ( $task) {//10 seconds after sending a message  $mail = new Mail ();  $to =  ' [email protected] ';  $content =  ' hello Workerman ';  $mail->sendlater ( $to, $ content);}; //run Workerworker::runall ();          
5, the static method of the timing function as a class
Require_once'./workerman/autoloader.php ';Use \Workerman\Worker;Use \Workerman\Lib\Timer;Classmail{Note that this is a static method, and the callback function property must also be publicPublicStaticfunctionSend($to,$content) {Echo"Send mail ... \ n"; }}$task =new Worker (); $task->onworkerstart = function($task) { //10 seconds after sending mail $to = ' [email protected] '; $content = ' Hello Workerman '; //Timed Call class static method Timer::add (( ' Mail ', ' send '), Array ($to, $content), false);}; //Run Workerworker::runall ();               
6. Static method (with namespace) for the class of the timer function
NamespaceTask;Require_once'./workerman/autoloader.php ';Use \Workerman\Worker;Use \Workerman\Lib\Timer;Classmail{Note that this is a static method, and the callback function property must also be publicPublicStaticfunctionSend($to,$content) {Echo"Send mail ... \ n"; }}$task =new Worker ();  $task->onworkerstart =  Function ( $task) {//10 seconds after sending a message Span class= "hljs-variable" > $to =  ' [email protected] ';  $content =  ' hello Workerman '; //the static method of calling class with namespace Timer::add (10, array ( ' \task\mail ',  ' send '), Span class= "Hljs-keyword" >array ( $to,  $content), false); //run Workerworker::runall ();          
7, the timer to destroy the current timer (use closure mode transfer $timer_id)
Use \Workerman\Worker;Use \Workerman\Lib\Timer;Require_once'./workerman/autoloader.php ';$task =New Worker ();$task->onworkerstart =function($task) {Count$count =1; //to $timer_id can be correctly passed to the inside of the callback function, $timer _id front must add the address character &  $timer _ id = timer::add (1, function () use (& $timer _id, & $count) {echo //run 10 times after destroying the current timer if ( $count + + >= 10) {echo  "Timer::d El ($timer _id) \ n "; Timer::d El ( $timer _id);});}; //run Workerworker::runall ();          
8, the timer to destroy the current timer (parameter mode transfer $timer_id)
Require_once'./workerman/autoloader.php ';Use \Workerman\Worker;Use \Workerman\Lib\Timer;Classmail{PublicfunctionSend($to,$content,$timer _id) {Temporarily adds a Count property to the current object to record the number of timer runs$this->count =Empty$this->count)?1:$this->count;Destroy the current timer after running 10 timesEcho"Send mail {$this->count}...\n";If$this->count++ >=10) {Echo"Timer::d El ($timer _id) \ n"; Timer::d El ( $timer _id); }}} $task = new Worker (); function  ( $task) { $mail = new Mail (); //to $timer_id can be correctly passed to the inside of the callback function, $timer _id front must add the address character &  $timer _ id = timer::add (1, array ( $mail, Span class= "hljs-string" > ' send '), array ( ' to ',  $timer _id);}; //run Workerworker::runall ();          
9. Set the timer only in the specified process

A worker instance has 4 processes, and the timer is set only on processes with ID number 0.

UseWorkerman\Worker;UseWorkerman\Lib\Timer;Require_once $worker = new Worker (); 4; $worker->onworkerstart = function  ( $worker) {//set timers only on processes with ID number 0 , other 1, 2, 3rd process does not set timer if ( $worker->id = = = 0) {timer::add (1, function () {echo " 4 worker processes, set timer \ n only in process # NO. 0; }); }}; //run Workerworker::runall ();          

Workerman Timer use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.