站長資訊網
        最全最豐富的資訊網站

        看看PHP 多進程處理任務

        看看PHP 多進程處理任務

        pcntl 模塊(非 Unix 類系統不支持此模塊)

        一個 PHP 多進程簡單例子大概是這個樣子:

        // 5 個子進程處理任務for ($i = 0; $i < 5; $i++) {     $pid = pcntl_fork();    if ($pid == -1) {        die("could not fork");     } elseif ($pid) {        echo "I'm the Parent $in";     } else { // 子進程處理         echo "I'm the Child $in";        // 業務處理         exit($i); // 一定要注意退出子進程,否則 pcntl_fork() 會被子進程再 fork,帶來處理上的影響。     } }// 等待子進程執行結束while (pcntl_waitpid(0, $status) != -1) {     $status = pcntl_wexitstatus($status);    echo "Child $status completedn"; }復制代碼

        當然實際應用中我們不能夠這樣輸出代碼,不夠健壯,也不夠優雅,我所以找了個基于 pcntl 封裝的擴展包來使用。

        spatie/async – 基于 pcntl 封裝的擴展包

        以下是我使用 spatie/async 來優化一個多進程請求的例子

        原代碼(耗時 20s 左右)- github.com/guanguans/m…

        看看PHP 多進程處理任務

        /**  * @param string $keyword  *  * @return array  */public function searchAll(string $keyword): array{     $songAll = [];    foreach ($this->platforms as $platform) {         $songAll = array_merge($songAll, $this->search($platform, $keyword));     }    return $songAll; }/**  * @param string $platform  * @param string $keyword  *  * @return mixed  */public function search(string $platform, string $keyword){     $meting = $this->getMeting($platform);      $songs = json_decode($meting->format()->search($keyword), true);    foreach ($songs as $key => &$song) {         $detail = json_decode($meting->format()->url($song['url_id']), true);        if (empty($detail['url'])) {            unset($songs[$key]);         }         $song = array_merge($song, $detail);     }    unset($song);    return $songs; }復制代碼

        改進后(耗時 4s 左右)- github.com/guanguans/m…

        看看PHP 多進程處理任務

        /**  * @param string $keyword  *  * @return array  */public function searchAll(string $keyword): array{     $songAll = [];     $pool = Pool::create();    foreach ($this->platforms as $platform) {         $pool->add(function () use ($platform, $keyword) {            return $this->search($platform, $keyword);         }, $this->getSerializedOutput())->then(function ($output) use (&$songAll) {             $songAll = array_merge($songAll, $output);         })->catch(function (Throwable $exception) {            exit($exception->getMessage());         });     }     $pool->wait();    return $songAll; }/**  * @return mixed  */public function search(string $platform, string $keyword){     $meting = $this->getMeting($platform);     $songs = json_decode($meting->format()->search($keyword), true);      $pool = Pool::create();    foreach ($songs as $key => &$song) {         $pool->add(function () use ($meting, $song) {            return json_decode($meting->format()->url($song['url_id']), true);         })->then(function ($output) use (&$songs, &$song, $key) {             $song = array_merge($song, $output);            if (empty($song['url'])) {                unset($songs[$key]);             }         })->catch(function (Throwable $exception) {            exit($exception->getMessage());         });     }    unset($song);     $pool->wait();    return $songs; }復制代碼

        想了解

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 日本五区在线不卡精品| 精品国产福利在线观看| 亚洲av午夜成人片精品网站| 亚洲国产精品一区| 人妻AV一区二区三区精品| 国产欧美精品一区二区三区四区| 国产精品国色综合久久| 亚洲国产综合精品一区在线播放| 一区二区三区日韩精品| 国产精品美脚玉足脚交欧美| 亚洲精品一品区二品区三品区| 国产精品午夜福利在线无码| 国产精品天天影视久久综合网| 亚洲AV日韩精品久久久久久久| 久久精品成人欧美大片| 97久久精品人人做人人爽| 国产成人精品久久二区二区| 久久99精品久久只有精品| 亚洲视频在线精品| 日韩福利视频精品专区| 久久国产精品二国产精品| 国产精品videossex白浆| 麻豆精品成人免费国产片| 18国产精品白浆在线观看免费| 精品人妻人人做人人爽| 日韩欧精品无码视频无删节 | 国产三级久久久精品麻豆三级| 亚洲精品国精品久久99热一| 亚洲国产精品日韩专区AV| 午夜在线视频91精品| 亚洲精品乱码久久久久久不卡| 久久久久亚洲精品无码网址 | 久久精品人人做人人妻人人玩 | 夜夜高潮夜夜爽国产伦精品| 亚洲国产成人精品无码久久久久久综合 | 欧美亚洲精品中文字幕乱码免费高清| 国内精品伊人久久久久影院对白 | 国产精品青草久久久久婷婷| 国产精品麻豆高清在线观看| 久久精品国产99国产精品澳门 | 国产高清精品在线|