账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    swoole协程的疑问
    39
    0

    遇到的问题

    我想在服务启动前,先链接到各个服务(例如redis服务),然后服务端收到信息后,我可以在里面调用对应的redis。

    但我在实例开发的时候,遇到以下报错。报错似乎提到 已经有其它协程在用导致发生错误

    这是不是可以理解为:在协程里,不能用相同的一个服务(变量),只能每次在go()里,重新连接一次?

    Fatal error: Uncaught SwooleError: Socket#4 has already been bound to another coroutine#505, reading of the same socket in coroutine#506

    新手上路,望指点。哈

    相关代码

    
    <?php
    use Swoole\Coroutine\Server;
    use Swoole\Coroutine\Server\Connection;
    
    class App
    {
        private static $app;
        private $server;
    
        private $mysql;
        private $redis;
    
        private function __construct()
        {
            go(function () {
                $this->redis = new Swoole\Coroutine\Redis();
                $this->redis->connect('redis', 6379);
            
                $server = new Server('0.0.0.0', 9502, false);
                $server->handle(function (Connection $conn) use ($server) {
                    while(true) {
                        $data = $conn->recv();
                        $json = json_decode($data, true);
                        var_dump($this->redis->get('1234'));
                    }
                });
                $server->start();
            });
    
            
        }
    
        public static function getInstance()
        {
            if (is_null(self::$app)) {
                self::$app = new self();
            }
    
            return self::$app;
        }
    
        public function run()
        {
            $this->server->start();
        }
    }
    
    App::getInstance();
    

    期待的结果

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部