Implement Timer task (Timer) in PHP only, and implement Timer timer_PHP tutorial in php

Source: Internet
Author: User
The Timer task (timer) is implemented in PHP only, and the Timer timer is implemented in php. Pure PHP implements Timer tasks (timer) and php implements Timer timer tasks, which are common in WEB applications. There are roughly two ways to implement Timer tasks using PHP: 1) use pure PHP to implement Timer tasks (timer), and php to implement Timer timer

Timer tasks are common in WEB applications. There are roughly two ways to implement timer tasks using PHP: 1) use the Crontab command to write a shell script and call the PHP file in the script, then execute the script regularly; 2) use ignore_user_abort () and set_time_limit () to run the script out of the browser. The former uses the features of Linux and does not have much to do with PHP itself. The latter has limited use cases and can only trigger the script once in an HTTP request. after the script is executed, it exits. So how can we use pure PHP to implement pure timer tasks and meet the business needs of task recognition?

Basic knowledge

This program is developed in Linux and runs in cli mode. here is a brief introduction to basic knowledge.

  • CLI: PHP command line mode. common WEB applications use fpm;
  • Processes: processes are the basic unit for running programs. they run independently and do not interfere with each other. they have independent runtime spaces and each process has a process control block;
  • Inter-process communication: Since processes run independently, we need a mechanism to ensure information exchange between different processes. Inter-process communication mainly includes: pipelines, IPC (shared memory,Signal, Message queue), socket;
  • PCNTL extension: a process extension of PHP. it mainly uses the pcntl_alarm () function. for details, refer to the official website.

Implementation principle    

Use a three-dimensional array to save all the tasks to be executed. The first-level index is the timestamp, the value is the method for executing the task, and the callback parameters. the specific array format is as follows:

Array ('20140901' => array (1, array ('class', 'func'), array (), true),) description: 1438156396 timestamp array (1, array ('class', 'func'), array (), true) parameters are represented in sequence: execution interval, callback function, whether the parameter passed to the callback function is persistent (true is always stored in the data; otherwise, it is deleted after execution)

These tasks can be methods of any class. Since it is a scheduled task, we need something similar to timing. this solution uses semaphores to send SIGALRM signals to the current process every second, capture the signals, and trigger the signal processing function, traverse data cyclically to determine whether there are tasks to be executed at the current time. If yes, the callback method is used and the parameter is passed to the method.

1
 $ Arr) 54 {55 $ current = time (); 56 57 foreach ($ arr as $ k => $ job) 58 {// traverse every task 59 $ func = $ job ['function'];/* callback function */60 $ argv = $ job ['argv']; /* callback function parameter */61 $ interval = $ job ['interval'];/* interval */62 $ persist = $ job ['persist']; /* persistence */63 64 if ($ current = $ time) 65 {// The current time has executed the task 66 67 // call the callback function, and pass the parameter 68 call_user_func_array ($ func, $ argv); 69 70 // delete this task 71 unset (self ::$ task [$ time] [$ k]); 72} 73 if ($ persist) 74 {// if persistence is performed, write the array and wait for the next awakening of 75 self :: $ task [$ current + $ interval] [] = $ job; 76} 77} 78 if (empty (self: $ task [$ time]) 79 {80 unset (self: $ task [$ time]); 81} 82} 83} 84 85/** 86 * add task 87 */88 public static function add ($ interval, $ func, $ argv = array (), $ persist = false) 89 {90 if (is_null ($ interval) 91 {92 return; 93} 94 $ time = time () + $ interval; 95 // write scheduled task 96 self: $ task [$ time] [] = array ('func' => $ func, 'argv' => $ argv, 'interval' => $ interval, 'persist' => $ persist); 97} 98 99/** 100 * delete all timer tasks 101 */102 public function dellAll () 103 {104 self ::$ task = array (); 105} 106}

This is the core part of the timer class. there is a static variable that stores all the tasks to be executed. why is it static here? Think for yourself. when the process receives the SIGALRM signal, it triggers the signalHandler function, and then traverses the array sequentially to check whether there are tasks to be executed at the current time. if yes, it calls back and passes parameters to delete the current job, then, check whether the persistence task is to be performed. If yes, continue to write the current job into the event array and wait for the next trigger. Finally, set an alarm signal for the current process. it can be seen that this timer will be triggered again from the internal as long as it is triggered once to get the self-loop purpose.

1
 

This is a callback class and function. for convenience, we have added a lot of debugging information. Timer classes and callbacks are available. let's take a look at the usage scenarios.

 1 
  1), false);12 13 echo "Time start: ".time()."\n";14 Timer::run();15 16 while(1)17 {18     sleep(1);19     pcntl_signal_dispatch();20 }

The code is very short. here two jobs are registered, and then the timer is run, in an infiniteCapture signals in a loopIf not captured, the pre-registered handler function cannot be triggered. the self-loop timer is developed successfully. the running result is as follows:

  

Like the tasks added in our scenario class, two tasks were executed at 90, one for a persistent job without parameters and the other for a non-persistent job with parameters, then the non-persistent job is not executed.

Summary

  • The current process cannot exit before receiving the signal. here I use a true loop with the condition always. in our actual production environment, we need to create such a prerequisite. for example, we have a group of services that are always running, regardless of IO access, waiting for socket links, etc, the current service will not be terminated, even if the process is blocked, there will be no problem. In this scenario, it is used in a continuously running service.
  • Currently, PHP only supports triggering in seconds. it does not support smaller time units. it is basically enough for scheduled tasks.

Timer (timer) and php implement Timer timer Timer tasks, which are common in WEB applications. There are roughly two ways to implement timer tasks using PHP: 1) 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.