
pcntl 模块(非 Unix 类系统不支持此模块)
一个 php 多进程简单例子大概是这个样子:
// 5 个子进程处理任务for ($i = 0; $i <p>当然实际应用中我们不能够这样输出代码,不够健壮,也不够优雅,我所以找了个基于 <code>pcntl</code> 封装的扩展包来使用。</p><h2 data-id="heading-1">spatie/async - 基于 <code>pcntl</code> 封装的扩展包</h2><p>以下是我使用 <code>spatie/async</code> 来优化一个多进程请求的例子</p><p>原代码(耗时 20s 左右)- github.com/guanguans/m…</p><p><img style="max-width:90%" alt="原代码" class="lazyload" src="https://img.php.cn/upload/article/000/000/052/70e7a17bf6613feb16481b733b941396-0.png?x-oss-process=image/resize,p_40" data-width="800" data- style="max-width:90%"></p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill5233" title="btpanel phpsite 宝塔面板PHP网站"><img
src="https://img.php.cn/upload/skill/000/000/081/179040786932301.jpg" alt="btpanel phpsite 宝塔面板PHP网站" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill5233" title="btpanel phpsite 宝塔面板PHP网站" class="overflowclass">btpanel phpsite 宝塔面板PHP网站</a>
<p class="overflowclass">宝塔面板 PHP 网站管理:站点创建、删除、启停、PHP 版本切换、域名管理、SSL证书管理、伪静态管理、数据库管理</p>
</div>
<a rel="nofollow" href="/xiazai/skill5233" title="btpanel phpsite 宝塔面板PHP网站" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><pre class="brush:php;toolbar:false;">/**
* @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…

/**
* @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;
}复制代码
想了解更多编程学习,敬请关注php培训栏目!
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










