详解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 相关文章推荐
3
Oct 09 PHP
十天学会php(1)
Oct 09 PHP
WML,Apache,和 PHP 的介绍
Oct 09 PHP
php处理json时中文问题的解决方法
Apr 12 PHP
php模拟用户自动在qq空间发表文章的方法
Jan 07 PHP
PHP模板引擎Smarty之配置文件在模板变量中的使用方法示例
Apr 11 PHP
CI框架简单邮件发送类实例
May 18 PHP
再谈PHP中单双引号的区别详解
Jun 12 PHP
ThinkPHP和UCenter接口冲突的解决方法
Jul 25 PHP
PHP + plupload.js实现多图上传并显示进度条加删除实例代码
Mar 06 PHP
PHP cookie与session会话基本用法实例分析
Nov 18 PHP
PHP高并发和大流量解决方案整理
Dec 24 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中常用的预定义变量小结
2012/05/09 PHP
调试WordPress中定时任务的相关PHP脚本示例
2015/12/10 PHP
tp5框架内使用tp3.2分页的方法分析
2019/05/05 PHP
jQuery为iframe的body添加click事件的实现代码
2011/04/07 Javascript
JavaScript获得url所有参数键值表的方法
2015/03/21 Javascript
JQuery球队选择实例
2015/05/18 Javascript
基于javascript实现最简单的选项卡切换效果
2016/05/16 Javascript
JavaScript数组操作函数汇总
2016/08/05 Javascript
Vue.js组件tabs实现选项卡切换效果
2016/12/01 Javascript
angularjs中的$eval方法详解
2017/04/24 Javascript
Vue.js对象转换实例
2017/06/07 Javascript
Angular X中使用ngrx的方法详解(附源码)
2017/07/10 Javascript
微信小程序使用input组件实现密码框功能【附源码下载】
2017/12/11 Javascript
video.js 实现视频只能后退不能快进的思路详解
2018/08/09 Javascript
浅谈从React渲染流程分析Diff算法
2018/09/08 Javascript
详解vue服务端渲染浏览器端缓存(keep-alive)
2018/10/12 Javascript
vue(2.x,3.0)配置跨域代理
2019/11/27 Javascript
[03:40]DOTA2亚洲邀请赛小组赛第二日 赛事回顾
2015/01/31 DOTA
python求素数示例分享
2014/02/16 Python
解决Linux系统中python matplotlib画图的中文显示问题
2017/06/15 Python
轻松理解Python 中的 descriptor
2017/09/15 Python
Python安装Flask环境及简单应用示例
2019/05/03 Python
使用 Python 读取电子表格中的数据实例详解
2020/04/17 Python
Python QTimer实现多线程及QSS应用过程解析
2020/07/11 Python
John Varvatos官方网站:设计师男士时装
2017/02/08 全球购物
介绍一些UNIX常用简单命令
2014/11/11 面试题
人事主管的岗位职责
2013/11/16 职场文书
绩效专员岗位职责
2013/12/02 职场文书
《月光启蒙》教学反思
2014/03/01 职场文书
团日活动总结怎么写
2014/06/25 职场文书
村干部群众路线教育活动对照检查材料
2014/10/01 职场文书
护士医德考评自我评价
2015/03/03 职场文书
2015年社区综治宣传月活动总结
2015/03/25 职场文书
《自己的花是让别人看的》教学反思
2016/02/19 职场文书
pyqt5打包成exe可执行文件的方法
2021/05/14 Python
nginx实现动静分离的方法示例
2021/11/07 Servers