看看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商城开发中如何构建完整的产品分类和商品管理系统?

    随着网络的普及和电子商务的快速发展,越来越多的商家开始将业务转移到网上。在这样的背景下,各种电商平台和商城应运而生。在构建一个商城的过程中,产品分类和商品管理系统的设计是非常重要的。作为一名PHP开发者…

    2023年5月19日
    01
  • PHP资源--RESOURCE

    说明:就是引用PHP外部的内容,这个时候的类型的就是资源。资源是通过专门的函数来建立和使用的。

    2017年11月28日
    0248
  • PHP入门指南:PHP和Prometheus。

    PHP作为一种开源的脚本语言,已经有20多年的历史。它主要被用于Web开发,特别是用于服务端的脚本。PHP的使用非常广泛,它被用于构建许多大型的Web应用程序和网站。Prometheus则是一种开源的监控系统和时间序列数据…

    2023年5月22日
    04
  • 字符串的布尔类型:bool, boolean

    bool和boolean用于标识某种只有两个状态值的数据:true,false——吃没吃,去没去,有没有。。。。。 在应用出,我们常常会(需要)直接将一个数据(可能是各种其他类型)当作一个布尔值来进行判断。 那么此时其实发…

    2018年3月17日
    0308
  • 我来教你php中else是什么意思,Php是什么意思。

    在PHP编程语言中,else是一个关键字,用于控制程序的流程,它通常与if语句一起使用,表示如果if条件不满足(即为假),则执行else后面的代码块,else语句可以单独使用,也可以与elseif(即else if)一起使用,以处理多个…

    2024年7月7日
    02
  • PHP开发的最佳SSH应用。

    PHP开发的最佳SSH应用在现代软件开发中,SSH已经成为了一种非常常见的协议,用于保护服务器和计算机之间的数据传输安全。SSH(Secure Shell)是一个网络协议,用于在计算机之间安全地进行数据传输和命令执行。在服…

    2023年5月28日
    00
  • PHP生成缩略图的方法实例(附代码)

    生成缩略图需要用到如下代码: $source_path:原图的路径 $NewImagePath:生成缩略图路径 $target_width:缩略图宽度 $target_height:缩略图高度 详细代码如下:  

    2018年2月28日
    0207
  • 详解PHP位运算符

    位运算符位运算符是指对二进制位从低位到高位对齐后进行运算。符号作用举例个人理解&按位与$m & $n全1为1,否则为0|按位或$m | $n全0为0,有1为1^按位异或$m | $n不同为1,相同为0~按位取反~$m<<向左…

    2022年6月27日
    099

联系我们

QQ:951076433

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