首页 / PHP / 看看PHP 多进程处理任务
看看PHP 多进程处理任务
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了看看PHP 多进程处理任务,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3028字,纯文字阅读大概需要5分钟。
内容图文
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 $i\n"; } else { // 子进程处理 echo "I'm the Child $i\n"; // 业务处理 exit($i); // 一定要注意退出子进程,否则 pcntl_fork() 会被子进程再 fork,带来处理上的影响。 } }// 等待子进程执行结束while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child $status completed\n"; }复制代码
当然实际应用中我们不能够这样输出代码,不够健壮,也不够优雅,我所以找了个基于 pcntl
封装的扩展包来使用。
spatie/async - 基于 pcntl
封装的扩展包
以下是我使用 spatie/async
来优化一个多进程请求的例子
原代码(耗时 20s 左右)- github.com/guanguans/m…
/** * @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 多进程处理任务的详细内容。
本文系统来源:https://www.php.cn/php-weizijiaocheng-460688.html内容总结
以上是互联网集市为您收集整理的看看PHP 多进程处理任务全部内容,希望文章能够帮你解决看看PHP 多进程处理任务所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。