看看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)
上一篇 2022年6月20日 00:18
下一篇 2022年6月20日 22:50

相关推荐

  • php构造函数的小结

    构造函数的小结 类定义的进一步完善

    2018年4月11日
    0185
  • PHP文件下载

    文件下载 如果下载的文件较多、文件大,通常就会使用百度云,如果下载的文件小、文件少的话,通常会使用php进行下载 通过php下载文件的原理: 先通过php读取下载的文件资源,读取到这些资源之后,再将其保存到文...

    2018年9月18日
    0337
  • (实用篇)PHP实现队列及队列原理

    队列说明 队列是一种线性表,按照先进先出的原则进行的: 实现队列 PHP实现队列:第一个元素作为队头,最后一个元素作为队尾 <?php /** * 队列就是这么简单 * * @link */ $array =  array('PHP', 'JAVA'); arr...

    2016年10月26日
    0246
  • 我的PHP学习第二十四天之PHP环境搭建

    什么是PHP? PHP是运行在服务器端的脚本语言,配合mysql和html实现动态网站。   脚本语言:编程语言有更加严格的规范。编程语言不能直接执行,需要编译后再执行。脚本文件可以直接被执行。 网站: 用户角度...

    2016年5月24日 PHP自学教程
    01.1K
  • 用PHP开发微信群发工具

    随着微信的普及,越来越多的企业开始将其作为营销工具。而微信群发功能,则是企业进行微信营销的重要手段之一。但是,如果只依靠手动发送,对于营销人员来说是一件极为费时费力的工作。所以,开发一款微信群发工...

    2023年5月18日
    02
  • PHP下ajax跨域的解决方案之window.name实例分析详解

    本文实例讲述了PHP下ajax跨域的解决方案之window.name。分享给大家供大家参考,具体如下:原理核心:window对象的name属性是一个很特别的属性,当该window的location变化,然后重新加载,它的name属性可以依然保...

    2022年6月15日
    0159
  • PHP中的财务系统开发指南。

    随着互联网时代的到来,许多企业开始转型发展,把业务扩展到互联网上,更好地满足用户需求。而财务系统作为一个至关重要的业务系统,也必须要跟随这个趋势进行升级和改造。本文将会为大家介绍PHP中的财务系统开发...

    2023年5月23日
    01
  • 解析基于php伪静态的实现方法

    一直在做php的开发工作.在开发的过程中老早就听说了“伪静态”这一说。但是一直没有对其进行了解。今天终于下定决定 要好好的了解下这方面的内容。首先,什么是伪静态:伪静态又名URL重写,是动态的网址看起来像静...

    2022年6月14日
    0127

联系我们

QQ:951076433

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