詳解PHP swoole process的使用方法
引入背景:假如我們每天有10000個(gè)訂單生成,需要同步到倉儲(chǔ)系統(tǒng)中去,以前做法是開啟一個(gè)crontab去跑這些任務(wù),但是發(fā)現(xiàn)總有感覺同步效率低,間隔時(shí)間都是分鐘級(jí)別的。
解決方案測(cè)試:我們將同步訂單的任務(wù)表添加一個(gè)hash作為key,作為分發(fā)條件,因?yàn)閙ysql中select如果做mod函數(shù)是用不到索引的,所以我們自己做隨機(jī)hash,但是務(wù)必不需要范圍太大,以免服務(wù)器資源不夠,方法是根據(jù)hashkey投放到不同的進(jìn)程中進(jìn)行同步,測(cè)試代碼如下
<?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} 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作為進(jìn)程間共享的內(nèi)存,為了分配index。以及當(dāng)進(jìn)程退出后,父進(jìn)程通過wait重新拉起該進(jìn)程任務(wù)。
測(cè)試截圖
進(jìn)程ps
結(jié)果 休眠20s后退出后會(huì)被自動(dòng)拉起
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PHP swoole中使用task進(jìn)程異步的處理耗時(shí)任務(wù)應(yīng)用案例分析
- php swoole多進(jìn)程/多線程用法示例【基于php7nts版】
- PHP基于swoole多進(jìn)程操作示例
- PHP swoole中http_server的配置與使用方法實(shí)例分析
- PHP使用swoole編寫簡單的echo服務(wù)器示例
- php安裝swoole擴(kuò)展的方法
- php異步多線程swoole用法實(shí)例
- PHP的swoole擴(kuò)展安裝方法詳細(xì)教程
- 使用swoole擴(kuò)展php websocket示例
- PHP框架Swoole定時(shí)器Timer特性分析
- PHP+swoole實(shí)現(xiàn)簡單多人在線聊天群發(fā)
- PHP swoole的process模塊創(chuàng)建和使用子進(jìn)程操作示例
相關(guān)文章
Yii Framework框架中事件和行為的區(qū)別及應(yīng)用實(shí)例分析
這篇文章主要介紹了Yii Framework框架中事件和行為的區(qū)別及應(yīng)用,結(jié)合實(shí)例形式分析了Yii Framework框架中事件和行為的相關(guān)概念、原理、區(qū)別及應(yīng)用操作技巧,需要的朋友可以參考下2020-04-04Zend?Framework基于Command命令行建立Zend?Framework項(xiàng)目的方法
這篇文章主要介紹了Zend?Framework基于Command命令行建立Zend?Framework項(xiàng)目的方法,較為詳細(xì)的分析了使用Command命令行建立Zend?Framework框架的具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-02-02CI框架源碼解讀之URI.php中_fetch_uri_string()函數(shù)用法分析
這篇文章主要介紹了CI框架源碼解讀之URI.php中_fetch_uri_string()函數(shù)用法,結(jié)合實(shí)例形式分析了CI框架中URL路由機(jī)制的原理與相關(guān)的config配置文件設(shè)置方法,需要的朋友可以參考下2016-05-05