Home > Article > Backend Development > Detailed explanation of PHP scheduled execution task implementation method (Timer), detailed explanation of timer_PHP tutorial
The following will share with you the implementation method of PHP scheduled execution task through theoretical explanation, example analysis, and effect display. .
Timer tasks are common in WEB applications. There are roughly two options for how to use PHP to implement timer tasks:
1) Use the Crontab command to write a shell script, call the PHP file in the script, and then execute the script regularly;
2) Use ignore_user_abort() and set_time_limit() together to make the script run outside the browser.
The former takes advantage of the characteristics of Linux and has little to do with PHP itself. The latter has limited usage scenarios and can only trigger the script with one HTTP request and exit after execution. So how do we use pure PHP to implement pure timer tasks and adapt to the business needs of cognitive tasks?
Basic knowledge
This program is developed under Linux and runs in cli mode. Here is a brief introduction to the basic knowledge.
CLI: PHP command line mode, common WEB applications use fpm;
Process: Process is the basic unit of program running. Processes run independently and do not interfere with each other. They have independent running spaces. Each process has a process control block;
Inter-process communication: Since processes run independently, we need a mechanism to ensure the exchange of information between different processes. Inter-process communication mainly includes: pipes, IPC (shared memory, signals, message queues), sockets;
PCNTL extension: A process extension of PHP, mainly using the pcntl_alarm() function. For detailed introduction, please refer to the official website.
Implementation principle
Use a three-dimensional array to save all tasks that need to be executed. The first-level index is the timestamp, and the value is the method of executing the task, callback parameters, etc. The specific array form is as follows:
Copy code The code is as follows:
array(
'1438156396' => array(
array(1,array('Class','Func'), array(), true),
)
)
Description:
1438156396 timestamp
Copy code The code is as follows:
array(1,array('Class','Func'), array(), true)
The parameters in turn represent: execution time interval, callback function, parameters passed to the callback function, whether to persist (true is always saved in the data, otherwise it is deleted after execution once)
These tasks can be methods of any class. Since it is a scheduled task, we need something similar to timing. This solution uses a semaphore to send the SIGALRM signal to the current process every second, capture the signal, trigger the signal processing function, loop through the data, and determine whether there is a current time required to perform the task. If so, use a callback to trigger it and pass the parameters to the method.
<?php /** *定时器 */ class Timer { //保存所有定时任务 public static $task = array(); //定时间隔 public static $time = 1; /** *开启服务 *@param $time int */ public static function run($time = null) { if($time) { self::$time = $time; } self::installHandler(); pcntl_alarm(1); } /** *注册信号处理函数 */ public static function installHandler() { pcntl_signal(SIGALRM, array('Timer','signalHandler')); } /** *信号处理函数 */ public static function signalHandler() { self::task(); //一次信号事件执行完成后,再触发下一次 pcntl_alarm(self::$time); } /** *执行回调 */ public static function task() { if(empty(self::$task)) {//没有任务,返回 return ; } foreach(self::$task as $time => $arr) { $current = time(); foreach($arr as $k => $job) {//遍历每一个任务 $func = $job['func']; /*回调函数*/ $argv = $job['argv']; /*回调函数参数*/ $interval = $job['interval']; /*时间间隔*/ $persist = $job['persist']; /*持久化*/ if($current == $time) {//当前时间有执行任务 //调用回调函数,并传递参数 call_user_func_array($func, $argv); //删除该任务 unset(self::$task[$time][$k]); } if($persist) {//如果做持久化,则写入数组,等待下次唤醒 self::$task[$current+$interval][] = $job; } } if(empty(self::$task[$time])) { unset(self::$task[$time]); } } } /** *添加任务 */ public static function add($interval, $func, $argv = array(), $persist = false) { if(is_null($interval)) { return; } $time = time()+$interval; //写入定时任务 self::$task[$time][] = array('func'=>$func, 'argv'=>$argv, 'interval'=>$interval, 'persist'=>$persist); } /** *删除所有定时器任务 */ public function dellAll() { self::$task = array(); } }
This is the core part of the timer class. There is a static variable that stores all the tasks that need to be executed. Why is it static here? Think about it for yourself. When the process receives the SIGALRM signal, the signalHandler function is triggered, and then the array is traversed sequentially. Check whether there are tasks that need to be executed at the current time. If there are any tasks that need to be executed at the current time, call back and pass parameters to delete the current job. Then check whether a persistent task is to be done. If so, continue to write the current job into the event array and wait for the next trigger. Finally, the current job will be deleted. The process sets an alarm signal. It can be seen that this timer, as long as it is triggered once, will be triggered again from the inside, achieving the purpose of self-loop.
<?php class DoJob { public function job( $param = array() ) { $time = time(); echo "Time: {$time}, Func: ".get_class()."::".__FUNCTION__."(".json_encode($param).")\n"; } }
This is the callback class and function. For the convenience of explanation, a lot of debugging information has been added. The Timer class and callback are available. Let’s see what the usage scenario is like.
<?php require_once(__DIR__."/Timer.php"); require_once(__DIR__."/DoJob.php"); Timer::dellAll(); Timer::add( 1, array('DoJob','job'), array(),true); Timer::add( 3, array('DoJob','job'),array('a'=>1), false); echo "Time start: ".time()."\n"; Timer::run(); while(1) { sleep(1); pcntl_signal_dispatch(); }
The code is very short. Two jobs are registered here, and then the timer is run to capture the signal triggering action in an infinite loop. If it is not captured, the pre-registered processing function will not be triggered. Such a self-loop timer has been developed. .The running results are as follows:
Just like the tasks added by our scene class, two tasks were executed at 90, one was a persistent job without parameters, and the other was a non-persistent job with parameters, and then the non-persistent job was no longer executed.
Summary
The current process cannot exit before receiving the signal. Here I use a loop whose condition is always true. In our actual production environment, we need to create such a prerequisite. For example, we have a set of services, and these services are It is always running. Whether it is IO access, waiting for socket connection, 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 service that is always running.
Currently, PHP only supports triggering in seconds and does not support smaller time units, which is basically enough for scheduled tasks
The above is the entire introduction to this article, I hope you all like it.