php workerman定时任务的实现代码
程序员文章站
2022-04-29 10:47:13
一、下载workerman
二、下载workerman/mysql
1、定时函数为匿名函数(闭包)
use \workerman\work...
一、下载workerman
二、下载workerman/mysql
1、定时函数为匿名函数(闭包)
use \workerman\worker; use \workerman\lib\timer; require_once './workerman/autoloader.php'; $task = new worker(); // 开启多少个进程运行定时任务,注意多进程并发问题 $task->count = 1; $task->onworkerstart = function($task) { // 每2.5秒执行一次 $time_interval = 2.5; timer::add($time_interval, function() { echo "task run\n"; }); }; // 运行worker worker::runall();
2、定时函数为普通函数
require_once './workerman/autoloader.php'; use \workerman\worker; use \workerman\lib\timer; // 普通的函数 function send_mail($to, $content) { echo "send mail ...\n"; } $task = new worker(); $task->onworkerstart = function($task) { $to = 'workerman@workerman.net'; $content = 'hello workerman'; // 10秒后执行发送邮件任务,最后一个参数传递false,表示只运行一次 timer::add(10, 'send_mail', array($to, $content), false); }; // 运行worker worker::runall();
3、定时函数为类的方法
require_once './workerman/autoloader.php'; use \workerman\worker; use \workerman\lib\timer; class mail { // 注意,回调函数属性必须是public public function send($to, $content) { echo "send mail ...\n"; } } $task = new worker(); $task->onworkerstart = function($task) { // 10秒后发送一次邮件 $mail = new mail(); $to = 'workerman@workerman.net'; $content = 'hello workerman'; timer::add(10, array($mail, 'send'), array($to, $content), false); }; // 运行worker worker::runall();
4、定时函数为类方法(类内部使用定时器)
require_once './workerman/autoloader.php'; use \workerman\worker; use \workerman\lib\timer; class mail { // 注意,回调函数属性必须是public public function send($to, $content) { echo "send mail ...\n"; } public function sendlater($to, $content) { // 回调的方法属于当前的类,则回调数组第一个元素为$this timer::add(10, array($this, 'send'), array($to, $content), false); } } $task = new worker(); $task->onworkerstart = function($task) { // 10秒后发送一次邮件 $mail = new mail(); $to = 'workerman@workerman.net'; $content = 'hello workerman'; $mail->sendlater($to, $content); }; // 运行worker worker::runall();
5、定时函数为类的静态方法
require_once './workerman/autoloader.php'; use \workerman\worker; use \workerman\lib\timer; class mail { // 注意这个是静态方法,回调函数属性也必须是public public static function send($to, $content) { echo "send mail ...\n"; } } $task = new worker(); $task->onworkerstart = function($task) { // 10秒后发送一次邮件 $to = 'workerman@workerman.net'; $content = 'hello workerman'; // 定时调用类的静态方法 timer::add(10, array('mail', 'send'), array($to, $content), false); }; // 运行worker worker::runall();
6、定时函数为类的静态方法(带命名空间)
namespace task; require_once './workerman/autoloader.php'; use \workerman\worker; use \workerman\lib\timer; class mail { // 注意这个是静态方法,回调函数属性也必须是public public static function send($to, $content) { echo "send mail ...\n"; } } $task = new worker(); $task->onworkerstart = function($task) { // 10秒后发送一次邮件 $to = 'workerman@workerman.net'; $content = 'hello workerman'; // 定时调用带命名空间的类的静态方法 timer::add(10, array('\task\mail', 'send'), array($to, $content), false); }; // 运行worker worker::runall();
7、定时器中销毁当前定时器(use闭包方式传递$timer_id)
use \workerman\worker; use \workerman\lib\timer; require_once './workerman/autoloader.php'; $task = new worker(); $task->onworkerstart = function($task) { // 计数 $count = 1; // 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 & $timer_id = timer::add(1, function()use(&$timer_id, &$count) { echo "timer run $count\n"; // 运行10次后销毁当前定时器 if($count++ >= 10) { echo "timer::del($timer_id)\n"; timer::del($timer_id); } }); }; // 运行worker worker::runall();
8、定时器中销毁当前定时器(参数方式传递$timer_id)
require_once './workerman/autoloader.php'; use \workerman\worker; use \workerman\lib\timer; class mail { public function send($to, $content, $timer_id) { // 临时给当前对象添加一个count属性,记录定时器运行次数 $this->count = empty($this->count) ? 1 : $this->count; // 运行10次后销毁当前定时器 echo "send mail {$this->count}...\n"; if($this->count++ >= 10) { echo "timer::del($timer_id)\n"; timer::del($timer_id); } } } $task = new worker(); $task->onworkerstart = function($task) { $mail = new mail(); // 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 & $timer_id = timer::add(1, array($mail, 'send'), array('to', 'content', &$timer_id)); }; // 运行worker worker::runall();
9、只在指定进程中设置定时器
一个worker实例有4个进程,只在id编号为0的进程上设置定时器。
use workerman\worker; use workerman\lib\timer; require_once './workerman/autoloader.php'; $worker = new worker(); $worker->count = 4; $worker->onworkerstart = function($worker) { // 只在id编号为0的进程上设置定时器,其它1、2、3号进程不设置定时器 if($worker->id === 0) { timer::add(1, function(){ echo "4个worker进程,只在0号进程设置定时器\n"; }); } }; // 运行worker worker::runall();
示例
shipments.php用来写定时任务
<?php /** * created by phpstorm. * user: administrator * date: 2018/11/29 * time: 16:59 */ use workerman\worker; use \workerman\lib\timer; require_once "workerman/autoloader.php"; require_once "connection.php"; $task = new worker(); $task->onworkerstart = function ($task) { global $db, $redis; $db = new \workerman\mysql\connection('127.0.0.1', '3306', 'root', 'root', 'test'); $redis = new redis(); $redis->connect('127.0.0.1', 6379); $redis->auth("qqq123123."); $time_interval = 0.1; timer::add($time_interval, function () { global $db, $redis; $insert['name'] = 123; $db->insert('shipments')->cols($insert)->query(); // sleep(100); }); }; function curlget($url = '', $options = []) { $ch = curl_init($url); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_timeout, 30); if (!empty($options)) { curl_setopt_array($ch, $options); } //https请求 不验证证书和host curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); $data = curl_exec($ch); curl_close($ch); return $data; } function newgetorderinfo($taobao, $orderid) { $taobao = urlencode($taobao); $url = "http://114.55.144.79/taobao/tradefullinfogetrequest.php?shop=$taobao&tid=$orderid"; $json = curlget($url); return json_decode($json, true)['trade']; } worker::runall();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 微信小程序日历/日期选择插件使用方法详解
下一篇: matlab-多项式乘除法及式子和导数