当前位置:首页 > PHP教程 > php高级应用 > 列表

php workerman定时任务的实现代码

发布:smiling 来源: PHP粉丝网  添加日期:2021-11-03 11:26:45 浏览: 评论:0 

这篇文章主要介绍了php workerman定时任务的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧

一、下载workerman

https://www.workerman.net/download

二、下载workerman/mysql

http://doc3.workerman.net/640201

1、定时函数为匿名函数(闭包)

  1. use \Workerman\Worker; 
  2. use \Workerman\Lib\Timer; 
  3. require_once './Workerman/Autoloader.php'; 
  4.  
  5. $task = new Worker(); 
  6. // 开启多少个进程运行定时任务,注意多进程并发问题 
  7. $task->count = 1; 
  8. $task->onWorkerStart = function($task) 
  9. { 
  10.   // 每2.5秒执行一次 
  11.   $time_interval = 2.5; 
  12.   Timer::add($time_interval, function() 
  13.   { 
  14.     echo "task run\n"; 
  15.   }); 
  16. }; 
  17.  
  18. // 运行worker 
  19. Worker::runAll(); 

2、定时函数为普通函数

  1. require_once './Workerman/Autoloader.php'; 
  2. use \Workerman\Worker; 
  3. use \Workerman\Lib\Timer; 
  4.  
  5. // 普通的函数 
  6. function send_mail($to, $content) 
  7. { 
  8.   echo "send mail ...\n"; 
  9. } 
  10.  
  11. $task = new Worker(); 
  12. $task->onWorkerStart = function($task) 
  13. { 
  14.   $to = 'workerman@workerman.net'; 
  15.   $content = 'hello workerman'; 
  16.   // 10秒后执行发送邮件任务,最后一个参数传递false,表示只运行一次 
  17.   Timer::add(10, 'send_mail', array($to, $content), false); 
  18. }; 
  19.  
  20. // 运行worker 
  21. Worker::runAll(); 

3、定时函数为类的方法

  1. require_once './Workerman/Autoloader.php'; 
  2. use \Workerman\Worker; 
  3. use \Workerman\Lib\Timer; 
  4.  
  5. class Mail 
  6. { 
  7.   // 注意,回调函数属性必须是public 
  8.   public function send($to, $content) 
  9.   { 
  10.     echo "send mail ...\n"; 
  11.   } 
  12. } 
  13.  
  14. $task = new Worker(); 
  15. $task->onWorkerStart = function($task) 
  16. { 
  17.   // 10秒后发送一次邮件 
  18.   $mail = new Mail(); 
  19.   $to = 'workerman@workerman.net'; 
  20.   $content = 'hello workerman'; 
  21.   Timer::add(10, array($mail, 'send'), array($to, $content), false); 
  22. }; 
  23.  
  24. // 运行worker 
  25. Worker::runAll(); 

4、定时函数为类方法(类内部使用定时器)

  1. require_once './Workerman/Autoloader.php'; 
  2. use \Workerman\Worker; 
  3. use \Workerman\Lib\Timer; 
  4.  
  5. class Mail 
  6. { 
  7.   // 注意,回调函数属性必须是public 
  8.   public function send($to, $content) 
  9.   { 
  10.     echo "send mail ...\n"; 
  11.   } 
  12.  
  13.   public function sendLater($to, $content) 
  14.   { 
  15.     // 回调的方法属于当前的类,则回调数组第一个元素为$this 
  16.     Timer::add(10, array($this, 'send'), array($to, $content), false); 
  17.   } 
  18. } 
  19.  
  20. $task = new Worker(); 
  21. $task->onWorkerStart = function($task) 
  22. { 
  23.   // 10秒后发送一次邮件 
  24.   $mail = new Mail(); 
  25.   $to = 'workerman@workerman.net'; 
  26.   $content = 'hello workerman'; 
  27.   $mail->sendLater($to, $content); 
  28. }; 
  29.  
  30. // 运行worker 
  31. Worker::runAll(); 

5、定时函数为类的静态方法

  1. require_once './Workerman/Autoloader.php'; 
  2. use \Workerman\Worker; 
  3. use \Workerman\Lib\Timer; 
  4.  
  5. class Mail 
  6. { 
  7.   // 注意这个是静态方法,回调函数属性也必须是public 
  8.   public static function send($to, $content) 
  9.   { 
  10.     echo "send mail ...\n"; 
  11.   } 
  12. } 
  13.  
  14. $task = new Worker(); 
  15. $task->onWorkerStart = function($task) 
  16. { 
  17.   // 10秒后发送一次邮件 
  18.   $to = 'workerman@workerman.net'; 
  19.   $content = 'hello workerman'; 
  20.   // 定时调用类的静态方法 
  21.   Timer::add(10, array('Mail', 'send'), array($to, $content), false); 
  22. }; 
  23.  
  24. // 运行worker 
  25. Worker::runAll(); 

6、定时函数为类的静态方法(带命名空间)

  1. namespace Task; 
  2. require_once './Workerman/Autoloader.php'; 
  3. use \Workerman\Worker; 
  4. use \Workerman\Lib\Timer; 
  5.  
  6. class Mail 
  7. { 
  8.   // 注意这个是静态方法,回调函数属性也必须是public 
  9.   public static function send($to, $content) 
  10.   { 
  11.     echo "send mail ...\n"; 
  12.   } 
  13. } 
  14.  
  15. $task = new Worker(); 
  16. $task->onWorkerStart = function($task) 
  17. { 
  18.   // 10秒后发送一次邮件 
  19.   $to = 'workerman@workerman.net'; 
  20.   $content = 'hello workerman'; 
  21.   // 定时调用带命名空间的类的静态方法 
  22.   Timer::add(10, array('\Task\Mail', 'send'), array($to, $content), false); 
  23. }; 
  24.  
  25. // 运行worker 
  26. Worker::runAll(); 

7、定时器中销毁当前定时器(use闭包方式传递$timer_id)

  1. use \Workerman\Worker; 
  2. use \Workerman\Lib\Timer; 
  3. require_once './Workerman/Autoloader.php'; 
  4.  
  5. $task = new Worker(); 
  6. $task->onWorkerStart = function($task) 
  7. { 
  8.   // 计数 
  9.   $count = 1; 
  10.   // 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 & 
  11.   $timer_id = Timer::add(1, function()use(&$timer_id, &$count) 
  12.   { 
  13.     echo "Timer run $count\n"; 
  14.     // 运行10次后销毁当前定时器 
  15.     if($count++ >= 10) 
  16.     { 
  17.       echo "Timer::del($timer_id)\n"; 
  18.       Timer::del($timer_id); 
  19.     } 
  20.   }); 
  21. }; 
  22.  
  23. // 运行worker 
  24. Worker::runAll(); 

8、定时器中销毁当前定时器(参数方式传递$timer_id)

  1. require_once './Workerman/Autoloader.php'; 
  2. use \Workerman\Worker; 
  3. use \Workerman\Lib\Timer; 
  4.  
  5. class Mail 
  6. { 
  7.   public function send($to, $content, $timer_id) 
  8.   { 
  9.     // 临时给当前对象添加一个count属性,记录定时器运行次数 
  10.     $this->count = emptyempty($this->count) ? 1 : $this->count; 
  11.     // 运行10次后销毁当前定时器 
  12.     echo "send mail {$this->count}...\n"; 
  13.     if($this->count++ >= 10) 
  14.     { 
  15.       echo "Timer::del($timer_id)\n"; 
  16.       Timer::del($timer_id); 
  17.     } 
  18.   } 
  19. } 
  20.  
  21. $task = new Worker(); 
  22. $task->onWorkerStart = function($task) 
  23. { 
  24.   $mail = new Mail(); 
  25.   // 要想$timer_id能正确传递到回调函数内部,$timer_id前面必须加地址符 & 
  26.   $timer_id = Timer::add(1, array($mail, 'send'), array('to', 'content', &$timer_id)); 
  27. }; 
  28.  
  29. // 运行worker 
  30. Worker::runAll(); 

9、只在指定进程中设置定时器

一个worker实例有4个进程,只在id编号为0的进程上设置定时器。

  1. use Workerman\Worker; 
  2. use Workerman\Lib\Timer; 
  3. require_once './Workerman/Autoloader.php'; 
  4.  
  5. $worker = new Worker(); 
  6. $worker->count = 4; 
  7. $worker->onWorkerStart = function($worker) 
  8. { 
  9.   // 只在id编号为0的进程上设置定时器,其它1、2、3号进程不设置定时器 
  10.   if($worker->id === 0) 
  11.   { 
  12.     Timer::add(1, function(){ 
  13.       echo "4个worker进程,只在0号进程设置定时器\n"; 
  14.     }); 
  15.   } 
  16. }; 
  17. // 运行worker 
  18. Worker::runAll(); 

示例

shipments.php用来写定时任务

  1. <?php 
  2. /** 
  3.  * Created by PhpStorm. 
  4.  * User: Administrator 
  5.  * Date: 2018/11/29 
  6.  * Time: 16:59 
  7.  */ 
  8.  
  9. use Workerman\Worker; 
  10. use \Workerman\Lib\Timer; 
  11.  
  12. require_once "Workerman/Autoloader.php"; 
  13.  
  14.  
  15. require_once "Connection.php"; 
  16.  
  17. $task = new Worker(); 
  18.  
  19. $task->onWorkerStart = function ($task) { 
  20.  
  21.   global $db, $redis; 
  22.   $db  = new \Workerman\MySQL\Connection('127.0.0.1', '3306', 'root', 'root', 'test'); 
  23.   $redis = new Redis(); 
  24.   $redis->connect('127.0.0.1', 6379); 
  25.   $redis->auth("qqq123123."); 
  26.   $time_interval = 0.1; 
  27.   Timer::add($time_interval, function () { 
  28.     global $db, $redis; 
  29.       
  30.     $insert['name'] = 123; 
  31.       
  32.     $db->insert('shipments')->cols($insert)->query(); 
  33.  
  34. //    sleep(100); 
  35.   }); 
  36.  
  37. }; 
  38.  
  39.  
  40. function curlGet($url = '', $options = []) 
  41. { 
  42.   $ch = curl_init($url); 
  43.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  44.   curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
  45.   if (!emptyempty($options)) { 
  46.     curl_setopt_array($ch, $options); 
  47.   } 
  48.   //https请求 不验证证书和host 
  49.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  50.   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
  51.   $data = curl_exec($ch); 
  52.   curl_close($ch); 
  53.   return $data; 
  54. } 
  55.  
  56. function newGetOrderInfo($taobao, $orderId) 
  57. { 
  58.   $taobao = urlencode($taobao); 
  59.   $url  = "http://114.55.144.79/taobao/TradeFullinfoGetRequest.php?shop=$taobao&tid=$orderId"; 
  60.   $json  = curlGet($url); 
  61.   return json_decode($json, true)['trade']; 
  62. } 
  63.  
  64. Worker::runAll();

Tags: workerman php定时任务

分享到: