当前位置:网站首页>谈一谈生产环境中swoole协程创建数量控制机制
谈一谈生产环境中swoole协程创建数量控制机制
2022-06-25 22:05:00 【ndrandy】
在swoole官方文档中,有提及“协程开销”。简单引用如下:
- 在
PHP-7.2版本中底层会分配8K的stack来存储协程的变量,zval的尺寸为16字节,因此8K的stack最大可以保存512个变量。协程栈内存占用超过8K后ZendVM会自动扩容。
PHP-7.1、PHP-7.0默认会分配256K栈内存
本文以php7.2为例,因为笔者当前使用的php版本也是7.2。
为了验证php7.2中,创建一个协程是否默认为8K,可以写个demo简单测试一下
<?php
$begin_memory = memory_get_usage();
go(function () {
co::sleep(10);
echo co::getCid() . PHP_EOL;
});
var_dump(memory_get_usage() - $begin_memory); //输出int(8640),单位是byte, 就是8K左右。既然创建一个协程,默认分配8K内存,假设我们在协程中执行的代码使用的栈内存,不超过8K,那么1G的内存大约可以创建多少个协程呢?1G= 1024MB = (1024 * 1024)KB, max_coroutine_num = 1024 * 1024 / 8 = 130072。也就是说1G内存就可以创建10w+的协程数量。相当的可观。但是实际情况,栈内存可能会超过。
笔者当前有个业务场景,从redis队列中pop数据,然后开协程写入mysql,我们知道mysql的写速度落后redis非常多,当业务高峰期,redis队列数据量猛增时,mysql达到瓶颈之后,协程的数量就会积压。高峰期持续时间达到一定程度,那么最终会因为内存资源不足,导致新的协程无法再创建,这时swoole会触发:PHP Warning: PHP Warning: go(): exceed max number of coroutine ...
所以得出一个经验:在生产消费模型中,最好对swoole 协程创建数量进行控制
swoole协程控制方案:
1、依赖swoole底层的max_coroutine配置。给出示例demo
<?php
set_error_handler(function () {
throw new Exception("error happen\n", 500);
});
co::set(['max_coroutine' => 1]);
try {
for ($i = 0; $i < 2; $i++) {
go(function () {
co::sleep(10);
});
}
} catch (\Throwable $e) {
if ($e->getCode() == 500) {
echo "协程数量超过限制!" . $e->getMessage();
}
}2、自己创建一个Context管理统计当前正在执行的协程数量,超过指定的数量,抛出异常或者自定义处理
<?php
define("APP_MAX_COROUTINE", 10);
$co_ctx = [];
go(function () use ($co_ctx) {
if (count($co_ctx) >= APP_MAX_COROUTINE) {
//超过限制,退出
return;
}
$cid = co::getCid();
defer(function () use ($cid, $co_ctx) {
if (isset($co_ctx[$cid])) {
unset($co_ctx[$cid]);
}
});
$co_ctx[$cid] = $cid;
//begin business ...
});
讨论结束,不正之处 ,多多指教! thks!!!
边栏推荐
- 权限设计=功能权限+数据权限
- Gradle的环境安装与配置
- CSDN原力值
- Hibernate entity class curd, transaction operation summary
- Extraction system apk
- The software test interview has been suspended. The interviewer always says that the logical thinking is chaotic. What should I do?
- QT custom implemented calendar control
- Today's 61 Fu
- 如何进行流程创新,以最经济的方式提升产品体验?
- 关于Swoole协程容器
猜你喜欢

Qt自定义实现的日历控件

录屏转gif的好用小工具ScreenToGif,免费又好用!

Kotlin空指针Bug

28 rounds of interviews with 10 companies in two and a half years (including byte, pinduoduo, meituan, Didi...)

Once beego failed to find bee after passing the go get command Exe's pit

What is Unified Extensible Firmware Interface (UEFI)?

Bi-sql stored procedure (I)

Extraction system apk

debezium

YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
随机推荐
OBS-Studio-27.2.4-Full-Installer-x64. Exe Download
My vscode
28 rounds of interviews with 10 companies in two and a half years (including byte, pinduoduo, meituan, Didi...)
Episode 3: thread synchronization using thread lock
Summary of common JDBC exceptions and error solutions
(serial port Lora module) centrida rf-al42uh private protocol test at instruction test communication process
谈一谈PHP变量或参数的Copy On Write机制
18亿像素火星全景超高清NASA放出,非常震撼
Kotlin null pointer bug
Qtcreator formatting code
实例:用C#.NET手把手教你做微信公众号开发(21)--使用微信支付线上收款:H5方式
转载: QTableWidget详解(样式、右键菜单、表头塌陷、多选等)
Record the ideas and precautions for QT to output a small number of pictures to mp4
二叉排序树
Solving typeerror: Unicode objects must be encoded before hashing
产品经理如何把控产品开发的进度
CSDN add on page Jump and off page specified paragraph jump
proxy
关于Swoole协程容器
STL教程5-STL基本概念及String和vector使用