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

        看看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无码一区二区男男| 亚洲AV无码久久精品狠狠爱浪潮| 午夜国产精品无套| 日韩精品无码永久免费网站| 九九久久精品无码专区| 国产精品青草视频免费播放| 国产精品综合久久第一页| 国产精品成| 国产精品欧美一区二区三区| 国产精品成人69XXX免费视频| 亚洲欧洲国产精品你懂的| 国内精品久久国产大陆| 91在线视频精品| 97精品国产手机| 国产精品莉莉欧美自在线线| 99久久免费国产精品热| 国产精品高清视亚洲精品| 国产精品成人va在线观看| www.久久精品| 青草青草久热精品视频在线网站 | 囯产精品一区二区三区线| 亚洲国产成人久久精品影视 | 国产在线精品一区二区不卡| 国产精品99久久精品| 久久99国产精品久久99| 久久青青草原国产精品免费| 欧美精品亚洲精品日韩专区va| 国产精品爽黄69天堂a| 欧美激情精品久久久久| 91国内揄拍国内精品情侣对白| 93精91精品国产综合久久香蕉 | 精品国产日产一区二区三区| 国产福利在线观看精品| 国产精品55夜色66夜色|