看看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和Haskell进行函数式编程。

    随着互联网的发展,编程语言也随之不断地更新和完善。如今,各种编程语言层出不穷,而其中PHP和Haskell这两种编程语言,都在开发者中备受关注。PHP是一种十分流行的服务器端脚本语言,用于快速开发Web应用程序。PHP…

    2023年5月30日
    01
  • 如何使用PHP实现微信小程序中的图片滚动操作。

    随着微信小程序的广泛使用,越来越多的开发者开始使用PHP来实现其中的各种功能。其中,图片滚动是微信小程序中常见的一种操作,下面就介绍如何使用PHP来实现微信小程序中的图片滚动操作。准备工作在开始之前,我们…

    2023年6月3日
    06
  • 创建 PSR-4 的 Php 包

    【相关学习推荐:php图文教程】本文是帮助初学者搭建基础的 php composer 包, 本项目源码地址githubpackagist目录结构和初建准备首先创建一个目录来存放所有文件, 这里我 命名为 util-demo , 目录中需要包含两个目…

    2022年6月21日
    0151
  • PHP8.0中的自动加载库:Composer

    作为一门流行的服务器端脚本语言,PHP已经发展了数十年。在这个过程中,它积累了大量的社区资源和第三方库,这些资源和库可以方便地应用于各种项目中。当然,如果要在PHP项目中重复使用这些库,解决依赖问题是至关…

    2023年5月19日
    02
  • 重蔚自留地php学习第四十二天——对象(面向对象)

    面向对象编程 面向对象编程不是技术,是一种思想而已。 面向对象编程:OOP(object oriented programming) 面向过程和面向对象 面向过程:将一个事务分成具体的某系列功能,然后将一连串的功能连起来使用,从而解…

    2018年12月24日 我php路线
    0316
  • PHP8.0中的trait组合

    随着PHP语言的不断发展和升级,trait(特征)这个概念也越来越被程序员所认知和广泛应用。在PHP8.0版本中,trait组合成为了一个非常有价值的特性,对于编写高质量、易维护的代码来说,这是至关重要的。在过去的版本…

    2023年5月18日
    011
  • 如何使用PHP和Docker构建可部署的应用。

    随着互联网的快速发展,越来越多的应用程序需要基于云服务器进行部署,而使用 Docker 容器技术对于实现这一目标来说是一个很好的选择。在本篇文章中,我们将介绍如何使用 PHP 和 Docker 构建可部署的应用程序,从而…

    2023年5月30日
    04
  • (理论篇)53个要点提高PHP编程效率

    用单引号代替双引号来包含字符串,这样做会更快一些。因为php会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说echo是语言…

    2016年10月23日
    0422

联系我们

QQ:951076433

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