成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導(dǎo)航首頁 ? 技術(shù)教程 ? 詳解PHP swoole process的使用方法
全站頭部文字 我要出現(xiàn)在這里
詳解PHP swoole process的使用方法 760 2023-12-10   

引入背景:假如我們每天有10000個訂單生成,需要同步到倉儲系統(tǒng)中去,以前做法是開啟一個crontab去跑這些任務(wù),但是發(fā)現(xiàn)總有感覺同步效率低,間隔時間都是分鐘級別的。

解決方案測試:我們將同步訂單的任務(wù)表添加一個hash作為key,作為分發(fā)條件,因?yàn)閙ysql中select如果做mod函數(shù)是用不到索引的,所以我們自己做隨機(jī)hash,但是務(wù)必不需要范圍太大,以免服務(wù)器資源不夠,方法是根據(jù)hashkey投放到不同的進(jìn)程中進(jìn)行同步,測試代碼如下

<?php
/**
 * Created by PhpStorm.
 * User: xujun
 * Date: 2017/8/26
 * Time: 9:37
 */
//假定需要處理的數(shù)據(jù)如下
class Process{
  public $mpid=0;
  public $max_precess=5;
  //代替從數(shù)據(jù)庫中讀取的內(nèi)容
  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);//用于父子進(jìn)程間數(shù)據(jù)交換
      $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)){//如果沒有指定了索引,新建的子進(jìn)程,開啟計(jì)數(shù)
      $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} Donen";
      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作為進(jìn)程間共享的內(nèi)存,為了分配index。以及當(dāng)進(jìn)程退出后,父進(jìn)程通過wait重新拉起該進(jìn)程任務(wù)。

測試截圖

進(jìn)程ps

查看圖片

結(jié)果 休眠20s后退出后會被自動拉起

查看圖片

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持綠夏網(wǎng)。


主站蜘蛛池模板: 误判电影什么时候上映| 幻乐森林演员表| 91精品在线视频播放| 日韩在线日韩| 1980属猴多少岁了| 迷妹网| 加藤なお| 热带雨林电影完整版播放| 南来北往电视剧演员表| 东方电视台节目表今日节目| 名剑风流 电视剧| 夜夜夜夜简谱| 听风者电视剧40集完整版在线观看| 26uuu电影网| 大幻术师| 守株待兔的老农夫音乐教案| 色在线视频| 张国荣身高| 蕾切尔薇兹牺牲最大的电影| 普庵咒全文注音版| 三年片大全电影| 来自地狱| 寻梦环游记英文| 电影《追求》| 欢迎观临| 广西都市频道节目表| 演员任贤齐简历| 伊利奶粉哪个系列最好| 极地特快电影英文版| 六年级上册脱式计算题100道| 美女撒尿视频| 避幕 电影| 东方卫视节目表| 我,喜欢你演员表介绍| 奥特曼格斗进化重生破解版| 老江湖| 阿尔法变频器说明书| 五年级脱式计算题100道| 情人电视剧| dnf代码大全| 抖音下截|

!!!站長長期在線接!!!

網(wǎng)站、小程序:定制開發(fā)/二次開發(fā)/仿制開發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103