Home
Document
Q&A
Video
Donate
Source Code
Code-Galaxy 云原生平台
Business
Swoole Tracker
Swoole Compiler
Login
Register
全部
提问
分享
讨论
建议
公告
开发框架
发表新帖
为什么连接池只创建一个连接
### 问题描述 用官网文档连接池示例代码,默认是创建64个链接,但是执行的过程中,我打开mysql,执行show full processlist;只看到一个mysql连接,请问这是什么原因 ### Swoole版本,PHP版本,以及操作系统版本信息 Swoole => 4.8.5 PHP => 8.1.7 系统 => Ubuntu 20.04.4 LTS ### 相关代码 ```php declare(strict_types=1); use Swoole\Coroutine; use Swoole\Database\PDOConfig; use Swoole\Database\PDOPool; use Swoole\Runtime; const N = 1024; Runtime::enableCoroutine(); $s = microtime(true); Coroutine\run(function () { $pool = new PDOPool((new PDOConfig) ->withHost('192.168.100.127') ->withPort(3306) //->withUnixSocket('/tmp/mysql.sock') ->withDbName('ems') ->withCharset('utf8') ->withUsername('root') ->withPassword('root') ); for ($n = N; $n--;) { Coroutine::create(function () use ($pool) { $pdo = $pool->get(); $statement = $pdo->prepare('select * from energystorageunit'); if (!$statement) { throw new RuntimeException('Prepare failed'); } $a = mt_rand(1, 100); $b = mt_rand(1, 100); $result = $statement->execute(); if (!$result) { throw new RuntimeException('Execute failed'); } $result = $statement->fetchAll(); var_dump($result); $pool->put($pdo); }); } while (true){ } }); $s = microtime(true) - $s; echo 'Use ' . $s . 's for ' . N . ' queries' . PHP_EOL; ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 期待的结果 show full processlist;有64个mysql连接 实际看到 show full processlist;只生成了一个mysql连接
发布于4周前 · 4 次浏览 · 来自
提问
十二月
### 问题描述 用官网文档连接池示例代码,默认是创建64个链接,但是执行的过程中,我打开mysql,执行show full processlist;只看到一个mysql连接,请问这是什么原因 ### Swoole版本,PHP版本,以及操作系统版本信息 Swoole => 4.8.5 PHP => 8.1.7 系统 => Ubuntu 20.04.4 LTS ### 相关代码 ```php declare(strict_types=1); use Swoole\Coroutine; use Swoole\Database\PDOConfig; use Swoole\Database\PDOPool; use Swoole\Runtime; const N = 1024; Runtime::enableCoroutine(); $s = microtime(true); Coroutine\run(function () { $pool = new PDOPool((new PDOConfig) ->withHost('192.168.100.127') ->withPort(3306) //->withUnixSocket('/tmp/mysql.sock') ->withDbName('ems') ->withCharset('utf8') ->withUsername('root') ->withPassword('root') ); for ($n = N; $n--;) { Coroutine::create(function () use ($pool) { $pdo = $pool->get(); $statement = $pdo->prepare('select * from energystorageunit'); if (!$statement) { throw new RuntimeException('Prepare failed'); } $a = mt_rand(1, 100); $b = mt_rand(1, 100); $result = $statement->execute(); if (!$result) { throw new RuntimeException('Execute failed'); } $result = $statement->fetchAll(); var_dump($result); $pool->put($pdo); }); } while (true){ } }); $s = microtime(true) - $s; echo 'Use ' . $s . 's for ' . N . ' queries' . PHP_EOL; ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 期待的结果 show full processlist;有64个mysql连接 实际看到 show full processlist;只生成了一个mysql连接
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
评论
2022-07-19
Rango
连接池的实现是在发生实际的请求是才会真正去创建连接。可以看源码。
赞
0
回复
微信公众号
热门内容
- swoole针对400的请求--TRACE Port_onRead_http() (ERRNO 7102): Bad Request: unknown protocol from session#193 on 0.0.0.0:9505
- 基于Swoole+PHP的超高性能短信网关(CMPP、SMPP)模拟器
- http服务中,使用write分段输出文件。设置header总文件长度无用
- Call to undefined function Swoole\Coroutine\run() 是怎么回事啊?
- Swoole Loader扩展 如何下载,https://business.swoole.com/trial/download/ 404
- 请教当使用request主动推送的时候,产生新的连接,及会马上关闭连接问题
- 为什么连接池只创建一个连接
- timer定时器,启动一下,突然执行了1万多次,这是怎么回事呢?
- 在swoole 中, 使用地址引用自身,造成内存增加
- windows 配置wss链接,链接不成功
暂无回复的问答
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 请问那个一键协程化的代码是放外面还是set里面
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。