当前位置:Gxlcms > PHP教程 > PHP多任务秒级定时器的实现方法

PHP多任务秒级定时器的实现方法

时间:2021-07-01 10:21:17 帮助过:31人阅读

描述

最近在公司部署crontab的时候,突发奇想是否可以用PHP去实现一个定时器,颗粒度到秒级就好,因为crontab最多到分钟级别,同时也调研了一下用PHP去实现的定时器还真不太多,Swoole 扩展里面到实现了一个毫秒级的定时器很高效,但毕竟不是纯PHP代码写的,所以最后还是考虑用PHP去实现一个定时器类,以供学习参考。

实现

在实现定时器代码的时候,用到了PHP系统自带的两个扩展

Pcntl - 多进程扩展 :

主要就是让PHP可以同时开启很多子进程,并行的去处理一些任务。

Spl - SplMinHeap - 小顶堆

一个小顶堆数据结构,在实现定时器的时候,采用这种结构效率还是不错的,插入、删除的时间复杂度都是 O(logN) ,像 libevent 的定时器也在 1.4 版本以后采用了这种数据结构之前用的是 rbtree,如果要是使用链表或者固定的数组,每次插入、删除可能都需要重新遍历或者排序,还是有一定的性能问题的。

流程

ee6901e9d45e395f3a4a47c8ba91cc0.png

说明

1、定义定时器结构,有什么参数之类的.

2、然后全部注册进我们的定时器类 Timer.

3、调用定时器类的monitor方法,开始进行监听.

4、监听过程就是一个while死循环,不断的去看时间堆的堆顶是否到期了,本来考虑每秒循环看一次,后来一想每秒循环看一次还是有点问题,如果正好在我们sleep(1)的时候定时器有到期的了,那我们就不能马上去精准执行,可能会有延时的风险,所以还是采用 usleep(1000) 毫秒级的去看并且也可以将进程挂起减轻 CPU 负载.

代码

  1. /***
  2. * Class Timer
  3. */
  4. class Timer extends SplMinHeap
  5. {
  6. /**
  7. * 比较根节点和新插入节点大小
  8. * @param mixed $value1
  9. * @param mixed $value2
  10. * @return int
  11. */
  12. protected function compare($value1, $value2)
  13. {
  14. if ($value1['timeout'] > $value2['timeout']) {
  15. return -1;
  16. }
  17. if ($value1['timeout'] < $value2['timeout']) {
  18. return 1;
  19. }
  20. return 0;
  21. }
  22. /**
  23. * 插入节点
  24. * @param mixed $value
  25. */
  26. public function insert($value)
  27. {
  28. $value['timeout'] = time() + $value['expire'];
  29. parent::insert($value);
  30. }
  31. /**
  32. * 监听
  33. * @param bool $debug
  34. */
  35. public function monitor($debug = false)
  36. {
  37. while (!$this->isEmpty()) {
  38. $this->exec($debug);
  39. usleep(1000);
  40. }
  41. }
  42. /**
  43. * 执行
  44. * @param $debug
  45. */
  46. private function exec($debug)
  47. {
  48. $hit = 0;
  49. $t1 = microtime(true);
  50. while (!$this->isEmpty()) {
  51. $node = $this->top();
  52. if ($node['timeout'] <= time()) {
  53. //出堆或入堆
  54. $node['repeat'] ? $this->insert($this->extract()) : $this->extract();
  55. $hit = 1;
  56. //开启子进程
  57. if (pcntl_fork() == 0) {
  58. empty($node['action']) ? '' : call_user_func($node['action']);
  59. exit(0);
  60. }
  61. //忽略子进程,子进程退出由系统回收
  62. pcntl_signal(SIGCLD, SIG_IGN);
  63. } else {
  64. break;
  65. }
  66. }
  67. $t2 = microtime(true);
  68. echo ($debug && $hit) ? '时间堆 - 调整耗时: ' . round($t2 - $t1, 3) . "秒\r\n" : '';
  69. }
  70. }

实例

  1. $timer = new Timer();
  2. //注册 - 3s - 重复触发
  3. $timer->insert(array('expire' => 3, 'repeat' => true, 'action' => function(){
  4. echo '3秒 - 重复 - hello world' . "\r\n";
  5. }));
  6. //注册 - 3s - 重复触发
  7. $timer->insert(array('expire' => 3, 'repeat' => true, 'action' => function(){
  8. echo '3秒 - 重复 - gogo' . "\r\n";
  9. }));
  10. //注册 - 6s - 触发一次
  11. $timer->insert(array('expire' => 6, 'repeat' => false, 'action' => function(){
  12. echo '6秒 - 一次 - hello xxxx' . "\r\n";
  13. }));
  14. //监听
  15. $timer->monitor(false);

执行结果

fc7b9bbe9fbd89a17f2087f6241d1c8.png

也测试过比较极端的情况,同时1000个定时器1s全部到期,时间堆全部调整完仅需 0.126s 这是没问题的,但是每调整完一个定时器就需要去开启一个子进程,这块可能比较耗时了,有可能1s处理不完这1000个,就会影响下次监听继续触发,但是不开启子进程,比如直接执行应该还是可以处理完的。。。。当然肯定有更好的方法,目前只能想到这样。

以上就是PHP多任务秒级定时器的实现方法的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行