Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis coroutine can't to see connections #5106

Open
theriskus opened this issue Jul 25, 2023 · 2 comments
Open

Redis coroutine can't to see connections #5106

theriskus opened this issue Jul 25, 2023 · 2 comments

Comments

@theriskus
Copy link

theriskus commented Jul 25, 2023

Hello, I need help

  1. What did you do? If possible, provide a simple script for reproducing the error.
run(function () use ($baseChannels, $ws) {
            $channels = [];
            foreach ($baseChannels as $channel) {
                $channels[] = Yii::$app->notify->getHashKey().':'.$channel;
            }
            $redis = new \Swoole\Coroutine\Redis();
            $redis->connect(Yii::$app->notify->param['ip'], Yii::$app->notify->param['port']);
            if ($redis->subscribe($channels)) // or use psubscribe
            {
                while ($msg = $redis->recv()) {
                list($type, $name, $result) = $msg;
                    if ($type == 'subscribe') {
                        print_r(['connections' => $this->ws->connections]);
                        print_r(['sub' => $this->ws->getClientList(0)]);
                        print_r(['connection_list' => $this->ws->connection_list() ]);
                    }
             }
});
  1. What did you expect to see?

I need to get a count connections.

  1. What did you see instead?

In this case, there is a subscription record, but there are no connections

  1. What version of Swoole are you using (show your php --ri swoole)?

swoole v4.8.13

  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ?

php 7.4

@jingjingxyk
Copy link
Contributor

jingjingxyk commented Jul 26, 2023

reference :

https://wiki.swoole.com/#/coroutine_client/init?id=%e5%8d%8f%e7%a8%8b%e5%ae%a2%e6%88%b7%e7%ab%af-lt-docsify-ignore-all-gt

https://wiki.swoole.com/#/coroutine_client/redis?id=%e5%8d%8f%e7%a8%8bredis%e5%ae%a2%e6%88%b7%e7%ab%af

https://wiki.swoole.com/#/runtime

example:

Co::set(['hook_flags' => SWOOLE_HOOK_TCP]);

Co\run(function() {
    for ($c = 100; $c--;) {
        go(function () {//创建100个协程
            $redis = new Redis();
            $redis->connect('127.0.0.1', 6379);//此处产生协程调度,cpu切到下一个协程,不会阻塞进程
            $redis->get('key');//此处产生协程调度,cpu切到下一个协程,不会阻塞进程
        });
    }
});

@jingjingxyk
Copy link
Contributor

Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]); // v4.4+版本使用此方法。
// 或
Swoole\Runtime::enableCoroutine($flags = SWOOLE_HOOK_ALL);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants