Home > Article > Backend Development > Implementation of PHP scheduled tasks_PHP tutorial
Recently, because the project needs to do some processing on the remote database regularly, we need to make something like a scheduled task here. After hard work, I finally implemented this planned task using PHP. Let’s share the implementation process this time.
This time using PHP to implement scheduled tasks mainly uses the three functions ignore_user_abort() set_time_limit(0) sleep().
The specific code is as follows:
<?php ignore_user_abort();//该函数的作用是当用户关掉浏览器后,PHP脚本也可以继续执行. set_time_limit(3000);// 通过set_time_limit(0)可以让程序无限制的执行下去 $interval=5;// 每隔5s运行 //方法1--死循环 do{ echo '测试'.time().'<br/>'; sleep($interval);// 等待5s }while(true); //方法2---sleep 定时执行 require_once './curlClass.php';//引入文件 $curl=new httpCurl('www.phpernote.com');//实例化 $stime=$curl->getmicrotime(); for($i=0;$i<=10;$i++){ echo '测试'.time().'<br/>'; sleep($interval);// 等待5s } ob_flush(); flush(); $etime=$curl->getmicrotime(); echo '<hr>'; echo round(($etime-stime),4);//程序执行时间
In the specific implementation process, I personally feel that the efficiency of PHP's scheduled task execution is not high. It is recommended that the work of scheduled task execution be left to the shell. Relatively speaking, this method is too reluctant to implement, and the shell It's professional level.