首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
swoole mysql 协程,请求数据得1-2s 和不用协程差不多,有啥问题吗,帮忙看看
### 问题描述 swoole mysql 协程写了个例子,请求数据时间得1-2s和不用协程的差不多,是写的有啥问题吗,帮忙看看 ### Swoole版本,PHP版本,以及操作系统版本信息 php7.3 swoole4.7 centos7.8 ### 相关代码 ```php <?php use Swoole\Runtime; use Swoole\Coroutine; use function Swoole\Coroutine\run; class MySQL { public function common() { $s = microtime(true); for ($c = 50; $c--;) { $pdo = new PDO('mysql:host=192.168.9.71;dbname=test;charset=utf8mb4', 'root', '123456'); $statement = $pdo->prepare('SELECT * FROM `test` WHERE id=1'); for ($n = 100; $n--;) { $statement->execute(); $res = $statement->fetch(); var_dump('a:' . $res['test']); } } for ($c = 50; $c--;) { $link = mysqli_connect('192.168.9.71', 'root', '123456', 'test'); for ($n = 100; $n--;) { $temp = mysqli_query($link, "SELECT * FROM `test` WHERE id=1"); $res = mysqli_fetch_array($temp); var_dump('b:' . $res['test']); } } echo 'use ' . (microtime(true) - $s) . ' s'; } public function go() { Runtime::enableCoroutine(); $s = microtime(true); run(function () { for ($c = 50; $c--;) { Coroutine::create(function () { $pdo = new PDO('mysql:host=192.168.9.71;dbname=test;charset=utf8mb4', 'root', '123456'); $statement = $pdo->prepare('SELECT * FROM `test` WHERE id=1'); for ($n = 100; $n--;) { $statement->execute(); $res = $statement->fetch(); var_dump('a:' . $res['test']); } }); } for ($c = 50; $c--;) { Coroutine::create(function () { $link = mysqli_connect('192.168.9.71', 'root', '123456', 'test'); for ($n = 100; $n--;) { $temp = mysqli_query($link, "SELECT * FROM `test` WHERE id=1"); $res = mysqli_fetch_array($temp); var_dump('b:' . $res['test']); } }); } }); echo 'use ' . (microtime(true) - $s) . ' s'; } } $mysql = new MySQL(); $mysql->common(); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 下面是 go() 执行结果 ... string(2) "a:" string(2) "a:" string(2) "b:" string(2) "b:" string(2) "b:" use 1.723815202713 s 下面是 common() 执行结果 ... string(2) "b:" string(2) "b:" string(2) "b:" string(2) "b:" string(2) "b:" use 2.5237798690796 s 区别是上面的应该用了异步,但是没有那么快。
发布于5年前 · 41 次浏览 · 来自
提问
phpbin
### 问题描述 swoole mysql 协程写了个例子,请求数据时间得1-2s和不用协程的差不多,是写的有啥问题吗,帮忙看看 ### Swoole版本,PHP版本,以及操作系统版本信息 php7.3 swoole4.7 centos7.8 ### 相关代码 ```php <?php use Swoole\Runtime; use Swoole\Coroutine; use function Swoole\Coroutine\run; class MySQL { public function common() { $s = microtime(true); for ($c = 50; $c--;) { $pdo = new PDO('mysql:host=192.168.9.71;dbname=test;charset=utf8mb4', 'root', '123456'); $statement = $pdo->prepare('SELECT * FROM `test` WHERE id=1'); for ($n = 100; $n--;) { $statement->execute(); $res = $statement->fetch(); var_dump('a:' . $res['test']); } } for ($c = 50; $c--;) { $link = mysqli_connect('192.168.9.71', 'root', '123456', 'test'); for ($n = 100; $n--;) { $temp = mysqli_query($link, "SELECT * FROM `test` WHERE id=1"); $res = mysqli_fetch_array($temp); var_dump('b:' . $res['test']); } } echo 'use ' . (microtime(true) - $s) . ' s'; } public function go() { Runtime::enableCoroutine(); $s = microtime(true); run(function () { for ($c = 50; $c--;) { Coroutine::create(function () { $pdo = new PDO('mysql:host=192.168.9.71;dbname=test;charset=utf8mb4', 'root', '123456'); $statement = $pdo->prepare('SELECT * FROM `test` WHERE id=1'); for ($n = 100; $n--;) { $statement->execute(); $res = $statement->fetch(); var_dump('a:' . $res['test']); } }); } for ($c = 50; $c--;) { Coroutine::create(function () { $link = mysqli_connect('192.168.9.71', 'root', '123456', 'test'); for ($n = 100; $n--;) { $temp = mysqli_query($link, "SELECT * FROM `test` WHERE id=1"); $res = mysqli_fetch_array($temp); var_dump('b:' . $res['test']); } }); } }); echo 'use ' . (microtime(true) - $s) . ' s'; } } $mysql = new MySQL(); $mysql->common(); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 下面是 go() 执行结果 ... string(2) "a:" string(2) "a:" string(2) "b:" string(2) "b:" string(2) "b:" use 1.723815202713 s 下面是 common() 执行结果 ... string(2) "b:" string(2) "b:" string(2) "b:" string(2) "b:" string(2) "b:" use 2.5237798690796 s 区别是上面的应该用了异步,但是没有那么快。
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2021-09-06
鲁飞
你数据库的连接限制呢
赞
0
回复
2021-09-07
phpbin
回复
鲁飞
一开始报连接限制,设置的是下面这个 set global max_connect_errors=1000000
赞
0
回复
2021-09-07
phpbin
回复
鲁飞
执行下面的官方的代码,是4s多 [root@localhost test]# php C.php use 4.2655529975891 s ```php <?php use Swoole\Runtime; use Swoole\Coroutine; use function Swoole\Coroutine\run; // 此行代码后,文件操作,sleep,Mysqli,PDO,streams等都变成异步IO,见'一键协程化'章节 Runtime::enableCoroutine(); # 表示开始协程 $s = microtime(true); // Swoole\Coroutine\run()见'协程容器'章节 run(function() { // i just want to sleep... for ($c = 100; $c--;) { Coroutine::create(function () { # 这里和使用 go() 函数是一样的效果 for ($n = 100; $n--;) { usleep(1000); } }); } // 10k file read and write for ($c = 100; $c--;) { Coroutine::create(function () use ($c) { $tmp_filename = "/tmp/test-{$c}.php"; for ($n = 100; $n--;) { $self = file_get_contents(__FILE__); file_put_contents($tmp_filename, $self); assert(file_get_contents($tmp_filename) === $self); } unlink($tmp_filename); }); } }); echo 'use ' . (microtime(true) - $s) . ' s'; ```
赞
0
回复
2021-09-07
phpbin
回复
鲁飞
set global max_connections=1000000
赞
0
回复
2021-09-07
phpbin
执行了几次,发现时间差不多,猜测一下原因 第一次是创建了新表,只有1条数据,所以两种方式执行起来,相差不大,也有可能是虚拟机的原因 第二次使用的表中有2000+数据,总数据量大概在 336.00KB 左右,不使用索引查询,执行起来有明显的差异 下面为 go 方法 ... string(4) "b:10" string(4) "a:10" string(4) "b:10" string(4) "b:10" string(4) "b:10" string(4) "a:10" string(4) "b:10" string(4) "b:10" use 5.1850039958954 s 下面为 common 方法 ... string(4) "b:10" string(4) "b:10" string(4) "b:10" string(4) "b:10" string(4) "b:10" string(4) "b:10" use 19.795286893845 s
赞
0
回复