Laravel Octane 和 Swoole 协程的使用分析

news/2025/2/12 23:22:39/

之前在工作中使用 Laravel Octane 的 concurrently 处理并发时,发现在队列和定时任务中不会触发并发效果。经过分析,作了如下猜测:队列和定时任务都属于一个独立的进程,与 Octane 服务无关,而 Octane concurrently 恰恰需要在 Octane 环境下才能运行。

后来通过代码进行环境检测和查看 php 的进程,证明猜想成立。

info('check env', ['served by octane' => isset($_SERVER['LARAVEL_OCTANE']) && ((int)$_SERVER['LARAVEL_OCTANE'] === 1),'on swoole server' => (extension_loaded('swoole') || extension_loaded('openswoole')) && app()->bound(Server::class)
]);

为了能够在任意代码中实现并发,我们研究参考了 Hyperf 框架关于协程的代码,然后抽取了如下两个类:

<?phpnamespace App\Services;use App\Exceptions\ParallelExecutionException;
use Laravel\Octane\Facades\Octane;
use Throwable;
use Swoole\Coroutine as Co;class Parallel
{protected array $callbacks = [];protected array $results = [];/*** @var Throwable[]*/protected array $throwables = [];public function add(callable $callable, $key = null): void{if (is_null($key)) {$this->callbacks[] = $callable;} else {$this->callbacks[$key] = $callable;}}public function wait(bool $throw = true): array{if (isset($_SERVER['LARAVEL_OCTANE']) && ((int)$_SERVER['LARAVEL_OCTANE'] === 1)) {return Octane::concurrently($this->callbacks, 300000);}app('log')->useLoggingLoopDetection(false);Co\run(function () {foreach ($this->callbacks as $key => $callback) {Co::create(function () use ($callback, $key) {try {$this->results[$key] = $callback();} catch (Throwable $throwable) {$this->throwables[$key] = $throwable;unset($this->results[$key]);}});}});if ($throw && ($throwableCount = count($this->throwables)) > 0) {$message = 'Detecting ' . $throwableCount . ' throwable occurred during parallel execution:' . PHP_EOL . $this->formatThrowAbles($this->throwables);$executionException = new ParallelExecutionException($message);$executionException->setResults($this->results);$executionException->setThrowAbles($this->throwables);unset($this->results, $this->throwables);throw $executionException;}app('log')->useLoggingLoopDetection(true);return $this->results;}private function formatThrowAbles(array $throwables): string{$output = '';foreach ($throwables as $key => $value) {$output .= sprintf('(%s) %s: %s' . PHP_EOL . '%s' . PHP_EOL, $key, get_class($value), $value->getMessage(), $value->getTraceAsString());}return $output;}
}
<?phpnamespace App\Exceptions;use RuntimeException;class ParallelExecutionException extends RuntimeException
{protected array $results = [];protected array $throwables = [];public function getResults(): array{return $this->results;}public function setResults(array $results): void{$this->results = $results;}public function getThrowAbles(): array{return $this->throwables;}public function setThrowAbles(array $throwables): array{return $this->throwables = $throwables;}
}

其中,第一个类的作用是检测系统是否运行在 Octane 环境下,是则调用Octane concurrently,否则就执行 Swoole 协程代码,使用起来也比较简单:

$parallel = new Parallel();
$parallel->add(fn() => $this->analysisStructure(), 'structure');
$parallel->add(fn() => $this->analysisHabit(), 'habit');
['structure' => $structure,'habit' => $habit,
] = $parallel->wait();

之所以没有完全使用 Swoole 协程,是因为相比之下,Octane 代码更加优雅,我们在期待着某天更新后,Octane concurrently 也能直接在队列中运行使用。

第二个类的作用比较简单,就是对协程中异常的一个定义。

另外在分析过程中,我们也发现了一个比较有意思的事情:
在这里插入图片描述

如图所示,当我在路由中运行检测代码时,Octane 和 Swoole Server 都为 true;在控制器中运行检测代码时,又只有 Octane 为true;为什么会有这样的区分?我个人猜测是 Octane 在将框架代码读进内存时,特意跳过了控制器中的代码,以避免数据更新不及时等问题。

有知道具体原因的小伙伴,欢迎留言探讨。


http://www.ppmy.cn/news/1368979.html

相关文章

Java - 获取汉字大写首字母输出

背景 有个项目需要将一批字符串的拼音首字母输出并大写&#xff0c;写了个工具类。 实现 需要引入外部jar。 <dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.1</version> </dep…

MyBatis 学习(六)之动态 SQL

目录 1 动态 SQL 介绍 2 if 标签 3 where 标签 4 set 标签 5 trim 标签 6 choose、when、otherwise 标签 7 foreach 标签 8 bind 标签 1 动态 SQL 介绍 动态 SQL 是 MyBatis 强大特性之一&#xff0c;极大的简化我们拼装 SQL 的操作。MyBatis 的动态 SQL 是基于 OGNL 的…

利用FFMPEG 将RTSP流的音频G711 转码为AAC 并 推流到RTMP

之前我们的视频转码项目中 是没有加入音频的 现在 需要加入音频 &#xff0c;由于RTMP只支持AAC的 音频流 而有的RTSP流的音频编码并不是AAC 大多数都是G711编码 还分为G711A 和G711U 之前用ffmpeg命令行可以直接 完成转码 并推送到RTMP 但是考虑到无法获取更详细的状…

2314576

☞ 通用计算机启动过程 1️⃣一个基础固件&#xff1a;BIOS 一个基础固件&#xff1a;BIOS→基本IO系统&#xff0c;它提供以下功能&#xff1a; 上电后自检功能 Power-On Self-Test&#xff0c;即POST&#xff1a;上电后&#xff0c;识别硬件配置并对其进行自检&#xff0c…

APS面试审核准备的常规问题

之前根据其他人的经验贴&#xff0c;准备了一些可能APS 面试审核可能会遇到的常规问题&#xff0c;现在简单分享一下。 一般会考虑到留学资金来源&#xff0c;在德国能不能顺利毕业&#xff1b;学的是什么专业内容之类的&#xff0c;判断去德国会不会好好学习&#xff1b;对德国…

Vue开发实例(七)Axios的安装与使用

说明&#xff1a; 如果只是在前端&#xff0c;axios常常需要结合mockjs使用&#xff0c;如果是前后端分离&#xff0c;就需要调用对应的接口&#xff0c;获取参数&#xff0c;传递参数&#xff1b;由于此文章只涉及前端&#xff0c;所以我们需要结合mockjs使用&#xff1b;由于…

repo介绍和安装

介绍 https://blog.devwiki.net/2023/11/27/Windows-repo.html 安装&#xff1a; https://blog.csdn.net/ysy950803/article/details/104188793