看看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 $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 多进程处理任务的文章就分享到这,如果对你有帮助欢迎继续关注我们哦

本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/41144.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
php学习php学习订阅用户
上一篇 2022年6月20日 00:18
下一篇 2022年6月20日 22:50

相关推荐

  • PHP 新手入门之数据类型

    标量数据类型:是数据结构中最基本单元,只能储存一个数据 布尔型:boolean 字符串型:string 浮点型: float 整型 : integer 两种复合类型: array() 数组 object 对象 俩种特殊类型 resource 资源型 null 空 判…

    2018年12月16日
    0272
  • PHP入门指南:代码重构。

    在写PHP代码时,重构是一个非常重要的过程。随着应用程序的增长,代码库会变得越来越庞大,难以阅读和维护。重构是为了解决这个问题,让代码更加模块化,并可以更好地组织和扩展。当我们重构代码时,需要考虑以下几…

    2023年5月30日
    06
  • PHP中使用Redis实现Skiplist。

    在Web开发中,PHP和Redis都拥有着广泛的应用场景。PHP作为Web开发的主要语言,而Redis则是一个高性能的内存数据库。Redis拥有着快速的读写能力和灵活的数据结构,其功能的强大性能被广泛地应用于缓存、队列、实时通…

    2023年5月21日
    03
  • PHP引用传值

    是指将一个变量的引用关系拷贝一份,然后赋值给另一个变量,即另一个变量也同样由该关系决定了指向某个数据: 代码示例: 图示如下: 赋值(传值)完之后,两个变量指向同样一个数据。 我们有知道,对变量的操作就…

    2017年11月5日 PHP自学教程
    0262
  • 小编教你php的api接口。

    在PHP中编写API接口涉及到多个步骤,包括设置服务器环境、创建数据库连接、构建SQL查询、处理结果以及返回适当的响应,以下是详细的技术介绍,帮助你了解如何用PHP编写查询API接口。 设置服务器环境 在开始编写API…

    2024年7月16日
    01
  • PHP实现邮件发送状态的实时查询功能。

    PHP实现邮件发送状态的实时查询功能随着电子邮件的广泛普及,邮件发送已经成为日常工作中不可或缺的一个环节。但是,由于网络问题、邮件服务器故障等原因,有时候邮件发送会失败。当邮件发送失败时,我们常常需要查…

    2023年5月23日
    02
  • PHP与数据库缓存的集成。

    随着互联网的发展,数据量与访问量的快速增长,有效地缓存已成为提高网站性能的重要方式。在Web应用程序中,数据库是必不可少的组成部分。为了减轻数据库的负载,促进网站性能的提升,我们需要将缓存技术与数据库集…

    2023年5月21日
    03
  • 一定要改掉 这5个PHP编程中的不良习惯!

    【相关学习推荐:php图文教程】这5个PHP编程中的不良习惯,一定要改掉 PHP世界上最好的语言!测试循环前数组是否为空?$items = [];// ...if (count($items) > 0) { foreach ($items as $item) { // process on …

    2022年6月21日
    0115

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息