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

相关推荐

  • PHPer都应当掌握的注释标记!

    前言注释标签在代码注释中的作用非常大,好的找注释标签可以让你在编程过程中有更好、更舒适的体验,所以我今天准备整理一下这些标记,通过图文的形式展示出来,一方面是为了自己对这些注释标签有一个汇总整理,另…

    2022年6月25日 PHP自学教程
    0132
  • PHP入门指南:代理模式。

    PHP入门指南:代理模式代理模式是一种常见的设计模式,它通过为其他对象提供一种代理来控制对这些对象的访问。代理对象充当了原始对象的中间商,为原始对象提供了一个可控制访问的通道。在这篇文章中,我们将介绍PH…

    2023年5月30日
    04
  • PHP商城的物流配送系统设计与实现。

    随着电商行业的不断发展壮大,物流配送系统已经成为了电商企业中不可或缺的一部分。在PHP商城开发中,物流配送系统的设计和实现显得尤为重要。通过合理的物流配送系统设计,可以提高顾客的购物体验,同时也可以节省…

    2023年5月23日
    08
  • PHP与数据备份的集成。

    当今互联网应用越来越成熟,各种业务处理都需要用到数据库来保存数据。而随着数据量不断增加,数据备份变得愈发重要。在PHP应用程序中,数据备份是必须考虑的问题之一,因此PHP与数据备份的集成也变得至关重要。首…

    2023年5月21日
    01
  • PHP文件操作相关函数

    bool copy ( string filename , string dest ) 复制文件 string filename:原文件 string dest :目标文件 bool unlink ( string filename ) 删除文件 string filename:要删除的文件 bool rename ( string oldname…

    2017年11月15日
    0357
  • 我来说说html5如何连接php。

    HTML5 本身是一种标记语言,用来构建网页的结构和内容,PHP 则是一种服务端的脚本语言,用于处理数据和逻辑,然后将结果传递给客户端,要将 HTML5 与 PHP 连接起来,通常意味着您想要在网页上显示由 PHP 脚本处理的…

    2024年6月25日
    01
  • PHP的文件上传原理说明

    文件上传的基本介绍、应用场景 点击上传时,照片会上传到哪里去? 当我们点击上传后会将图片、文件等上传到服务器上面,并返回图片的地址,这样,我们只需要给其他用户提供该文件的地址即可。 文件上传的原理说明 …

    2018年9月15日 PHP自学教程
    0281
  • 关于php preg_replace_callback回调函数传参问题

    preg_replace_callback这个函数的作用是执行一个正则表达式搜索并且使用一个回调进行替换preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]…

    2022年6月25日
    0130

联系我们

QQ:951076433

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