PHP swoole process的使用

來源:互聯網
上載者:User

標籤:函數   set   test   分享   arc   name   完成   系統   array   

引入背景:假如我們每天有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

結果 休眠20s後退出後會被自動拉起

 

PHP swoole process的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.