当前位置:Gxlcms > php框架 > 详解PHP swoole process的使用方法

详解PHP swoole process的使用方法

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

引入背景:假如我们每天有10000个订单生成,需要同步到仓储系统中去,以前做法是开启一个crontab去跑这些任务,但是发现总有感觉同步效率低,间隔时间都是分钟级别的。

解决方案测试:我们将同步订单的任务表添加一个hash作为key,作为分发条件,因为mysql中select如果做mod函数是用不到索引的,所以我们自己做随机hash,但是务必不需要范围太大,以免服务器资源不够,方法是根据hashkey投放到不同的进程中进行同步,测试代码如下

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xujun
  5. * Date: 2017/8/26
  6. * Time: 9:37
  7. */
  8. //假定需要处理的数据如下
  9. class Process{
  10. public $mpid=0;
  11. public $max_precess=5;
  12. //代替从数据库中读取的内容
  13. public $task = [
  14. ['uid'=>1,'uname'=>'bot','hash'=>1,'handle'=>'test'],
  15. ['uid'=>2,'uname'=>'bot1','hash'=>2,'handle'=>'test'],
  16. ['uid'=>3,'uname'=>'bot2','hash'=>3,'handle'=>'test'],
  17. ['uid'=>4,'uname'=>'bot3','hash'=>4,'handle'=>'test'],
  18. ['uid'=>2,'uname'=>'bot4','hash'=>2,'handle'=>'test'],
  19. ['uid'=>3,'uname'=>'bot5','hash'=>3,'handle'=>'test'],
  20. ['uid'=>4,'uname'=>'bot6','hash'=>1,'handle'=>'test'],
  21. ];
  22. public $works = [];
  23. public $swoole_table = NULL;
  24. //public $new_index=0;
  25. function test($index,$task){
  26. print_r("[".date('Y-m-d H:i:s')."]".'work-index:'.$index.'处理'.$task['uname'].'完成'.PHP_EOL);
  27. }
  28. public function __construct(){
  29. try {
  30. $this->swoole_table = new swoole_table(1024);
  31. $this->swoole_table->column('index', swoole_table::TYPE_INT);//用于父子进程间数据交换
  32. $this->swoole_table->create();
  33. swoole_set_process_name(sprintf('php-ps:%s', 'master'));
  34. $this->mpid = posix_getpid();
  35. $this->run();
  36. $this->processWait();
  37. }catch (\Exception $e){
  38. die('ALL ERROR: '.$e->getMessage());
  39. }
  40. }
  41. public function run(){
  42. for ($i=0; $i < $this->max_precess; $i++) {
  43. $this->CreateProcess();
  44. }
  45. }
  46. private function getTask($index){
  47. $_return = [];
  48. foreach ($this->task as $v){
  49. if($v['hash']==$index){
  50. $_return[] = $v;
  51. }
  52. }
  53. return $_return;
  54. }
  55. public function CreateProcess($index=null){
  56. if(is_null($index)){//如果没有指定了索引,新建的子进程,开启计数
  57. $index=$this->swoole_table->get('index');
  58. if($index === false){
  59. $index = 0;
  60. }else{
  61. $index = $index['index']+1;
  62. }
  63. print_r($index);
  64. }
  65. $this->swoole_table->set('index',array('index'=>$index));
  66. $process = new swoole_process(function(swoole_process $worker)use($index){
  67. swoole_set_process_name(sprintf('php-ps:%s',$index));
  68. $task = $this->getTask($index);
  69. foreach ($task as $v){
  70. call_user_func_array(array($this,$v['handle']),array($index,$v));
  71. }
  72. sleep(20);
  73. }, false, false);
  74. $pid=$process->start();
  75. $this->works[$index]=$pid;
  76. return $pid;
  77. }
  78. public function rebootProcess($ret){
  79. $pid=$ret['pid'];
  80. $index=array_search($pid, $this->works);
  81. if($index!==false){
  82. $index=intval($index);
  83. $new_pid=$this->CreateProcess($index);
  84. echo "rebootProcess: {$index}={$new_pid} Done\n";
  85. return;
  86. }
  87. throw new \Exception('rebootProcess Error: no pid');
  88. }
  89. public function processWait(){
  90. while(1) {
  91. if(count($this->works)){
  92. $ret = swoole_process::wait();
  93. if ($ret) {
  94. $this->rebootProcess($ret);
  95. }
  96. }else{
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. $process = new Process();

这里代码中,使用了swoole_table作为进程间共享的内存,为了分配index。以及当进程退出后,父进程通过wait重新拉起该进程任务。

测试截图

进程ps

结果 休眠20s后退出后会被自动拉起

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行