国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

swoole_process實現(xiàn)進程池的方法示例

2024-05-05 00:05:45
字體:
供稿:網(wǎng)友

swoole —— 重新定義PHP

swoole 的進程之間有兩種通信方式,一種是消息隊列(queue),另一種是管道(pipe),對swoole_process 的研究在swoole中顯得尤為重要。

預備知識

IO多路復用

swoole 中的io多路復用表現(xiàn)為底層的 epoll進程模型,在C語言中表現(xiàn)為 epoll 函數(shù)。

  • epoll 模型下會持續(xù)監(jiān)聽自己名下的素有socket 描述符 fd
  • 當觸發(fā)了 socket 監(jiān)聽的事件時,epoll 函數(shù)才會響應,并返回所有監(jiān)聽該時間的 socket 集合
  • epoll 的本質(zhì)是阻塞IO,它的優(yōu)點在于能同事處理大量socket連接

Event loop 事件循環(huán)

swoole 對 epoll 實現(xiàn)了一個Reactor線程模型封裝,設置了read事件和write事件的監(jiān)聽回調(diào)函數(shù)。(詳見swoole_event_add)

  • Event loop 是一個Reactor線程,其中運行了一個epoll實例。
  • 通過swoole_event_add將socket描述符的一個事件添加到epoll監(jiān)聽中,事件發(fā)生時將執(zhí)行回調(diào)函數(shù)
  • 不可用于fpm環(huán)境下,因為fpm在任務結(jié)束時可能會關(guān)掉進程。

swoole_process

  • 基于C語言封裝的進程管理模塊,方便php來調(diào)用
  • 內(nèi)置管道、消息隊列接口,方便實現(xiàn)進程間通信

我們在php-fpm.conf配置文件中發(fā)現(xiàn),php-fpm中有兩種進程池管理設置。

  • 靜態(tài)模式 即初始化固定的進程數(shù),當來了一個請求時,從中選取一個進程來處理。
  • 動態(tài)模式 指定最小、最大進程數(shù),當請求量過大,進程數(shù)不超過最大限制時,新增線程去處理請求

接下來用swoole代碼來實現(xiàn),這里只是為理解swoole_process、進程間通信、定時器等使用,實際情況使用封裝好的swoole_server來實現(xiàn)task任務隊列池會更方便。

假如有個定時投遞的任務隊列:

<?php/** * 動態(tài)進程池,類似fpm * 動態(tài)新建進程 * 有初始進程數(shù),最小進程數(shù),進程不夠處理時候新建進程,不超過最大進程數(shù) */// 一個進程定時投遞任務/** * 1. tick * 2. process及其管道通訊 * 3. event loop 事件循環(huán) */class processPool{  private $pool;  /**   * @var swoole_process[] 記錄所有worker的process對象   */  private $workers = [];  /**   * @var array 記錄worker工作狀態(tài)   */  private $used_workers = [];  /**   * @var int 最小進程數(shù)   */  private $min_woker_num = 5;  /**   * @var int 初始進程數(shù)   */  private $start_worker_num = 10;  /**   * @var int 最大進程數(shù)   */  private $max_woker_num = 20;  /**   * 進程閑置銷毀秒數(shù)   * @var int   */  private $idle_seconds = 5;  /**   * @var int 當前進程數(shù)   */  private $curr_num;  /**   * 閑置進程時間戳   * @var array   */  private $active_time = [];  public function __construct()  {    $this->pool = new swoole_process(function () {      // 循環(huán)建立worker進程      for ($i = 0; $i < $this->start_worker_num; $i++) {        $this->createWorker();      }      echo '初始化進程數(shù):' . $this->curr_num . PHP_EOL;      // 每秒定時往閑置的worker的管道中投遞任務      swoole_timer_tick(1000, function ($timer_id) {        static $count = 0;        $count++;        $need_create = true;        foreach ($this->used_workers as $pid => $used) {          if ($used == 0) {            $need_create = false;            $this->workers[$pid]->write($count . ' job');            // 標記使用中            $this->used_workers[$pid] = 1;            $this->active_time[$pid] = time();            break;          }        }        foreach ($this->used_workers as $pid => $used)          // 如果所有worker隊列都沒有閑置的,則新建一個worker來處理          if ($need_create && $this->curr_num < $this->max_woker_num) {            $new_pid = $this->createWorker();            $this->workers[$new_pid]->write($count . ' job');            $this->used_workers[$new_pid] = 1;            $this->active_time[$new_pid] = time();          }        // 閑置超過一段時間則銷毀進程        foreach ($this->active_time as $pid => $timestamp) {          if ((time() - $timestamp) > $this->idle_seconds && $this->curr_num > $this->min_woker_num) {            // 銷毀該進程            if (isset($this->workers[$pid]) && $this->workers[$pid] instanceof swoole_process) {              $this->workers[$pid]->write('exit');              unset($this->workers[$pid]);              $this->curr_num = count($this->workers);              unset($this->used_workers[$pid]);              unset($this->active_time[$pid]);              echo "{$pid} destroyed/n";              break;            }          }        }        echo "任務{(diào)$count}/{$this->curr_num}/n";        if ($count == 20) {          foreach ($this->workers as $pid => $worker) {            $worker->write('exit');          }          // 關(guān)閉定時器          swoole_timer_clear($timer_id);          // 退出進程池          $this->pool->exit(0);          exit();        }      });    });    $master_pid = $this->pool->start();    echo "Master $master_pid start/n";    while ($ret = swoole_process::wait()) {      $pid = $ret['pid'];      echo "process {$pid} existed/n";    }  }  /**   * 創(chuàng)建一個新進程   * @return int 新進程的pid   */  public function createWorker()  {    $worker_process = new swoole_process(function (swoole_process $worker) {      // 給子進程管道綁定事件      swoole_event_add($worker->pipe, function ($pipe) use ($worker) {        $data = trim($worker->read());        if ($data == 'exit') {          $worker->exit(0);          exit();        }        echo "{$worker->pid} 正在處理 {$data}/n";        sleep(5);        // 返回結(jié)果,表示空閑        $worker->write("complete");      });    });    $worker_pid = $worker_process->start();    // 給父進程管道綁定事件    swoole_event_add($worker_process->pipe, function ($pipe) use ($worker_process) {      $data = trim($worker_process->read());      if ($data == 'complete') {        // 標記為空閑//        echo "{$worker_process->pid} 空閑了/n";        $this->used_workers[$worker_process->pid] = 0;      }    });    // 保存process對象    $this->workers[$worker_pid] = $worker_process;    // 標記為空閑    $this->used_workers[$worker_pid] = 0;    $this->active_time[$worker_pid] = time();    $this->curr_num = count($this->workers);    return $worker_pid;  }}new processPool();

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


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 睢宁县| 丁青县| 平阴县| 韶关市| 绥阳县| 河间市| 东源县| 灵山县| 卢龙县| 阿拉善左旗| 昌平区| 绥宁县| 麟游县| 会昌县| 江门市| 朔州市| 司法| 哈密市| 新田县| 沁水县| 宾阳县| 随州市| 宣化县| 娄底市| 朝阳区| 盐山县| 龙游县| 沅陵县| 海安县| 保靖县| 微山县| 东光县| 宁海县| 盐池县| 元谋县| 永顺县| 沙洋县| 哈巴河县| 当雄县| 兴山县| 赤峰市|