首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
使用swoole官方的连接池RedisPool,依然还是报了redis实例混用的错误
### 问题描述 代码第14行报错: PHP Fatal error: Uncaught Swoole\Error: Socket#9 has already been bound to another coroutine#4229695, reading of the same socket in coroutine#4229696 at the same time is not allowed. 意思是在不同的协程里使用了同一个redis链接,可我使用的就是swoole官方提供的协程高级-redis连接池,怎么还会报这么错误?是我代码写错了么? ### Swoole版本,PHP版本,以及操作系统版本信息 php 7.3.21 Swoole 4.6.4 ### 相关代码 ```php #Redis连接池 $redisPool = new RedisPool((new RedisConfig) ->withHost($env['CACHE']['HOST']) ->withPort((int)$env['CACHE']['PORT']) ->withAuth('') ->withDbIndex(0) ->withTimeout(1) ); #监听应用中心-消费券状态 Swoole\Timer::tick(200, function($timer_id) use ($chan,$pdoPool,$redisPool,$env){ $cname = $env['CACHE']['PREFIX'].'marketing-coupon-state'; try { $redis = $redisPool->get(); $dataJson = (string)$redis->rPop($cname); $dataArray = @json_decode($dataJson, true); if(!empty($dataArray) && is_array($dataArray)){ if (time() >= $dataArray['etime'] && $dataArray['state'] < 3){ $state = 3; #已结束 $chan->push(['type'=>'marketing-coupon-state','data'=>['id'=>$dataArray['id'],'state'=>$state]]); }elseif (time() > $dataArray['stime'] && time() < $dataArray['etime'] && $dataArray['state'] < 2){ $state = 2; #进行中 $chan->push(['type'=>'marketing-coupon-state','data'=>['id'=>$dataArray['id'],'state'=>$state]]); }else { #不满足任意条件,重新写入队列 $redis->lpush($cname, $dataJson); } } } catch (\Exception $e) { abelog($e); } catch (\Error $e) { abelog($e); } if(!empty($redis))$redisPool->put($redis); }); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 帮忙看一下到底是什么原因
发布于5年前 · 43 次浏览 · 来自
提问
白色的小空
### 问题描述 代码第14行报错: PHP Fatal error: Uncaught Swoole\Error: Socket#9 has already been bound to another coroutine#4229695, reading of the same socket in coroutine#4229696 at the same time is not allowed. 意思是在不同的协程里使用了同一个redis链接,可我使用的就是swoole官方提供的协程高级-redis连接池,怎么还会报这么错误?是我代码写错了么? ### Swoole版本,PHP版本,以及操作系统版本信息 php 7.3.21 Swoole 4.6.4 ### 相关代码 ```php #Redis连接池 $redisPool = new RedisPool((new RedisConfig) ->withHost($env['CACHE']['HOST']) ->withPort((int)$env['CACHE']['PORT']) ->withAuth('') ->withDbIndex(0) ->withTimeout(1) ); #监听应用中心-消费券状态 Swoole\Timer::tick(200, function($timer_id) use ($chan,$pdoPool,$redisPool,$env){ $cname = $env['CACHE']['PREFIX'].'marketing-coupon-state'; try { $redis = $redisPool->get(); $dataJson = (string)$redis->rPop($cname); $dataArray = @json_decode($dataJson, true); if(!empty($dataArray) && is_array($dataArray)){ if (time() >= $dataArray['etime'] && $dataArray['state'] < 3){ $state = 3; #已结束 $chan->push(['type'=>'marketing-coupon-state','data'=>['id'=>$dataArray['id'],'state'=>$state]]); }elseif (time() > $dataArray['stime'] && time() < $dataArray['etime'] && $dataArray['state'] < 2){ $state = 2; #进行中 $chan->push(['type'=>'marketing-coupon-state','data'=>['id'=>$dataArray['id'],'state'=>$state]]); }else { #不满足任意条件,重新写入队列 $redis->lpush($cname, $dataJson); } } } catch (\Exception $e) { abelog($e); } catch (\Error $e) { abelog($e); } if(!empty($redis))$redisPool->put($redis); }); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 帮忙看一下到底是什么原因
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2021-09-23
鲁飞
共用了一个连接就会报这个错误, https://wiki.swoole.com/#/question/use?id=%e6%98%af%e5%90%a6%e5%8f%af%e4%bb%a5%e5%85%b1%e7%94%a81%e4%b8%aaredis%e6%88%96mysql%e8%bf%9e%e6%8e%a5
赞
0
回复
2021-09-27
白色的小空
回复
鲁飞
可是我代码中用的就是官方提供的redis连接池,每个协程都是$redisPool->get()现取现用的不应该出现这个错误阿,难道说连接池里只有一个redis链接?
赞
0
回复
2021-09-29
鲁飞
回复
白色的小空
可能你连接没用完你就还回去了 导致另外一个协程获取到
赞
0
回复