看看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

相关推荐

  • (理论篇)localhost与127.0.0.1的区别

    很多人会接触到这个ip地址127.0.0.1。也许你会问127.0.0.1是什么地址?其实127.0.0.1是一个回送地址,指本地机,一般用来测试使用。大家常用来ping 127.0.0.1来看本地ip/tcp正不正常,如能ping通即可正常使用。 对…

    2016年10月24日
    0289
  • PHP入门指南:计算机网络。

    计算机网络是当今掌握互联网技术必备的基础知识之一。PHP作为一种常用于网站开发的脚本语言,也需要深入理解网络知识。本篇文章将带您一步步了解计算机网络的基础知识与PHP的应用。 一、计算机网络基础知识 网络的…

    2023年5月22日
    00
  • PHP8.0中的电子邮件服务库:Mailgun

    近年来,PHP成为了Web开发世界中的一大主流。无论是开发Web应用程序,构建API还是建立电子商务网站,PHP都是开发者们的首选语言。然而,即使是PHP,要为它构建一个完整的Web应用程序也需要很多额外的工作。其中之一…

    2023年5月18日
    01
  • 使用PHP8中的array_chunk()函数高效处理数组分块。

    PHP是一种广泛使用的脚本语言,它的主要用途是开发Web应用程序。它支持面向对象编程,能够很好地与HTML和HTTP协议结合使用,可以创建功能强大的Web应用程序。 在PHP8版本中,array_chunk()函数是一个非常有用的函数…

    2023年5月21日
    02
  • PHP入门指南:PHP和Haskell。

    PHP和Haskell是两种非常不同的编程语言,它们的设计思路和用途都不同。在本文中,我们将会介绍PHP和Haskell,以及它们各自的优缺点。同时,我们还将分别介绍如何入门这两种编程语言,并给出一些学习的建议。 PHP是…

    2023年5月22日
    00
  • PHP数组的介绍

    说明:一组(一堆)数据的集合,把多个数据进行组合,数组是由“元素”组成。 题:有6头牛,求牛的总体重和平均体重。 改进成数组的方式:

    2018年4月7日
    0407
  • PHP中的物联网。

    近年来随着物联网技术的不断发展,越来越多的应用场景涌现出来,许多企业也加速了其在物联网领域的布局。而PHP作为一种流行的编程语言,同样也在不断地发展和拓展。本文将讨论PHP在物联网领域的运用。 物联网是指通…

    2023年5月28日
    017
  • PHP的cookie技术详解

    Cookie介绍 Cookie是客户端技术,当客户端 请求服务器的时候,随身携带数据过去例如:我们去超市购物,买很多东西,超市会给我们办会员卡,会员卡就会保存我们购买的商品信息,以后我们只需要拿着会员卡就可以 Cook…

    2018年9月13日 PHP自学教程
    0265

联系我们

QQ:951076433

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