详解PHP swoole process的使用方法


Posted in PHP onAugust 26, 2017

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

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

<?php
/**
 * Created by PhpStorm.
 * User: xujun
 * Date: 2017/8/26
 * Time: 9:37
 */
//假定需要处理的数据如下
class Process{
  public $mpid=0;
  public $max_precess=5;
  //代替从数据库中读取的内容
  public $task = [
    ['uid'=>1,'uname'=>'bot','hash'=>1,'handle'=>'test'],
    ['uid'=>2,'uname'=>'bot1','hash'=>2,'handle'=>'test'],
    ['uid'=>3,'uname'=>'bot2','hash'=>3,'handle'=>'test'],
    ['uid'=>4,'uname'=>'bot3','hash'=>4,'handle'=>'test'],
    ['uid'=>2,'uname'=>'bot4','hash'=>2,'handle'=>'test'],
    ['uid'=>3,'uname'=>'bot5','hash'=>3,'handle'=>'test'],
    ['uid'=>4,'uname'=>'bot6','hash'=>1,'handle'=>'test'],
  ];
  public $works = [];
  public $swoole_table = NULL;
  //public $new_index=0;
  function test($index,$task){
    print_r("[".date('Y-m-d H:i:s')."]".'work-index:'.$index.'处理'.$task['uname'].'完成'.PHP_EOL);
  }

  public function __construct(){
    try {
      $this->swoole_table = new swoole_table(1024);
      $this->swoole_table->column('index', swoole_table::TYPE_INT);//用于父子进程间数据交换
      $this->swoole_table->create();

      swoole_set_process_name(sprintf('php-ps:%s', 'master'));
      $this->mpid = posix_getpid();
      $this->run();
      $this->processWait();
    }catch (\Exception $e){
      die('ALL ERROR: '.$e->getMessage());
    }
  }

  public function run(){
    for ($i=0; $i < $this->max_precess; $i++) {
      $this->CreateProcess();
    }
  }

  private function getTask($index){
    $_return = [];
    foreach ($this->task as $v){
      if($v['hash']==$index){
        $_return[] = $v;
      }
    }
    return $_return;
  }

  public function CreateProcess($index=null){
    if(is_null($index)){//如果没有指定了索引,新建的子进程,开启计数
      $index=$this->swoole_table->get('index');
      if($index === false){
        $index = 0;
      }else{
        $index = $index['index']+1;
      }
      print_r($index);
    }
    $this->swoole_table->set('index',array('index'=>$index));
    $process = new swoole_process(function(swoole_process $worker)use($index){

      swoole_set_process_name(sprintf('php-ps:%s',$index));
      $task = $this->getTask($index);
      foreach ($task as $v){
        call_user_func_array(array($this,$v['handle']),array($index,$v));
      }
      sleep(20);
    }, false, false);
    $pid=$process->start();

    $this->works[$index]=$pid;
    return $pid;
  }

  public function rebootProcess($ret){
    $pid=$ret['pid'];
    $index=array_search($pid, $this->works);
    if($index!==false){
      $index=intval($index);
      $new_pid=$this->CreateProcess($index);
      echo "rebootProcess: {$index}={$new_pid} Done\n";
      return;
    }
    throw new \Exception('rebootProcess Error: no pid');
  }

  public function processWait(){
    while(1) {
      if(count($this->works)){
        $ret = swoole_process::wait();
        if ($ret) {
          $this->rebootProcess($ret);
        }
      }else{
        break;
      }
    }
  }

}
$process = new Process();

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

测试截图

进程ps

详解PHP swoole process的使用方法

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

详解PHP swoole process的使用方法

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

PHP 相关文章推荐
使用Apache的htaccess防止图片被盗链的解决方法
Apr 27 PHP
PHP+jQuery实现自动补全功能源码
May 15 PHP
Codeigniter操作数据库表的优化写法总结
Jun 12 PHP
常见php数据文件缓存类汇总
Dec 05 PHP
给ECShop添加最新评论
Jan 07 PHP
php将图片文件转换成二进制输出的方法
Jun 10 PHP
隐性调用php程序的方法
Jun 13 PHP
PHP实现批量上传单个文件
Dec 29 PHP
php 开发中加密的几种方法总结
Mar 22 PHP
PHP实现将多个文件中的内容合并为新文件的方法示例
Jun 10 PHP
Laravel模糊查询区分大小写的实例
Sep 29 PHP
PHP pthreads v3使用中的一些坑和注意点分析
Feb 21 PHP
Yii2框架可逆加密简单实现方法
Aug 25 #PHP
PHP5.6新增加的可变函数参数用法分析
Aug 25 #PHP
php变量与JS变量实现不通过跳转直接交互的方法
Aug 25 #PHP
CodeIgniter整合Smarty的方法详解
Aug 25 #PHP
PHP观察者模式原理与简单实现方法示例
Aug 25 #PHP
PHP实现的策略模式简单示例
Aug 25 #PHP
php实现简单的权限管理的示例代码
Aug 25 #PHP
You might like
php中强制下载文件的代码(解决了IE下中文文件名乱码问题)
2011/05/09 PHP
php学习之流程控制实现代码
2011/06/09 PHP
php数组函数序列之rsort() - 对数组的元素值进行降序排序
2011/11/02 PHP
PHP使用pdo实现事务处理操作示例
2018/09/05 PHP
基于JQuery实现相同内容合并单元格的代码
2011/01/12 Javascript
AJAX分页的代码(后台asp.net)
2011/02/14 Javascript
jquery模拟按下回车实现代码
2011/09/20 Javascript
JQuery select(下拉框)操作方法汇总
2015/04/15 Javascript
JS实现超精简响应鼠标显示二级菜单代码
2015/09/12 Javascript
微信小程序 开发指南详解
2016/09/27 Javascript
jquery插件ContextMenu设置右键菜单
2017/03/13 Javascript
ES5学习教程之Array对象
2017/04/01 Javascript
React学习笔记之条件渲染(一)
2017/07/02 Javascript
jQuery实现的鼠标滚轮控制图片缩放功能实例
2017/10/14 jQuery
微信小程序之swiper滑动面板用法示例
2018/12/04 Javascript
[01:08:56]DOTA2-DPC中国联赛 正赛 Magma vs LBZS BO3 第一场 2月7日
2021/03/11 DOTA
详解Python中的Numpy、SciPy、MatPlotLib安装与配置
2017/11/17 Python
Python多进程并发与多线程并发编程实例总结
2018/02/08 Python
python 删除字符串中连续多个空格并保留一个的方法
2018/12/22 Python
Python面向对象程序设计多继承和多态用法示例
2019/04/08 Python
numpy.where() 用法详解
2019/05/27 Python
python argparser的具体使用
2019/11/10 Python
matlab中imadjust函数的作用及应用举例
2020/02/27 Python
python爬虫爬取网页数据并解析数据
2020/09/18 Python
使用phonegap进行本地存储的实现方法
2017/03/31 HTML / CSS
技校教师求职简历的自我评价
2013/10/20 职场文书
30年同学聚会邀请函
2014/01/25 职场文书
教师师德演讲稿
2014/05/06 职场文书
产品发布会策划方案
2014/05/12 职场文书
文秘自荐信
2014/06/28 职场文书
2014年最新个人对照检查材料范文
2014/09/25 职场文书
软弱涣散基层党组织整改方案
2014/10/25 职场文书
客户答谢会致辞
2015/01/20 职场文书
2014年个人总结范文
2015/03/09 职场文书
2015年秋学期教研工作总结
2015/10/14 职场文书
nginx location优先级的深入讲解
2021/03/31 Servers