溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP多進(jìn)程處理任務(wù)的案例

發(fā)布時間:2020-10-09 17:15:05 來源:億速云 閱讀:219 作者:小新 欄目:編程語言

這篇文章主要介紹了PHP多進(jìn)程處理任務(wù)的案例,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

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

一個 PHP 多進(jìn)程簡單例子大概是這個樣子:

// 5 個子進(jìn)程處理任務(wù)for ($i = 0; $i < 5; $i++) {
    $pid = pcntl_fork();    if ($pid == -1) {        die("could not fork");
    } elseif ($pid) {        echo "I'm the Parent $i\n";
    } else { // 子進(jìn)程處理
        echo "I'm the Child $i\n";        // 業(yè)務(wù)處理
        exit($i); // 一定要注意退出子進(jìn)程,否則 pcntl_fork() 會被子進(jìn)程再 fork,帶來處理上的影響。
    }
}// 等待子進(jìn)程執(zhí)行結(jié)束while (pcntl_waitpid(0, $status) != -1) {
    $status = pcntl_wexitstatus($status);    echo "Child $status completed\n";
}

當(dāng)然實(shí)際應(yīng)用中我們不能夠這樣輸出代碼,不夠健壯,也不夠優(yōu)雅,我所以找了個基于 pcntl 封裝的擴(kuò)展包來使用。

spatie/async - 基于 pcntl 封裝的擴(kuò)展包

以下是我使用 spatie/async 來優(yōu)化一個多進(jìn)程請求的例子

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

PHP多進(jìn)程處理任務(wù)的案例

/**
 * @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;
}

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

PHP多進(jìn)程處理任務(wù)的案例

/**
 * @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;
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享PHP多進(jìn)程處理任務(wù)的案例內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI