How to use RedisPool to manage several Redis connections with Swoole websocket

507 Views Asked by At

I have a Swoole websocket server and I would like to use a RedisPool to manage several Redis connections to pick one for each query when I get a message. When I try to get a connection from my RedisPool, Swoole outputs an error "Error: Uncaught Swoole\Error: API must be called in the coroutine in @swoole-src/library/core/ConnectionPool.php:69", so I tried to encapsulate my query with Co\Run :

Co\run(function() use ($key, $pool) {
    go(function () use ($key, $pool) {
        $redis = $pool->get();
        $result = $redis->get($key);

        $pool->put($redis);
    });
});

Then I get "Warning: Swoole\Coroutine\Scheduler::start(): eventLoop has already been created. unable to start Swoole\Coroutine\Scheduler"

I suppose the eventLoop has already been started by my websocket server. Is there a way to access the server eventLoop or another method to run a Coroutine ?

0

There are 0 best solutions below