企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## Channel 连接池 ## 示例 ``` <?php use Swoole\Coroutine; use Swoole\Coroutine\Redis as SwooleCoroutineRedis; use Swoole\Coroutine\Channel; // 创建连接池 $pool = new Channel(2); // 创建 Swoole 协程 Coroutine\run(function () use ($pool) { // 创建 Redis 连接并放入连接池 for ($i = 0; $i < 2; $i++) { $redis = new SwooleCoroutineRedis(); $redis->connect('127.0.0.1', 6379); $pool->push($redis); } // 协程1:从连接池中获取 Redis 连接并查询数据 Coroutine::create(function () use ($pool) { $redis = $pool->pop(); $result1 = $redis->get('key1'); echo "Result from coroutine 1: $result1\n"; // 将连接放回连接池 $pool->push($redis); }); // 协程2:从连接池中获取 Redis 连接并查询数据 Coroutine::create(function () use ($pool) { $redis = $pool->pop(); $result2 = $redis->get('key2'); echo "Result from coroutine 2: $result2\n"; // 将连接放回连接池 $pool->push($redis); }); // 防止协程退出 Coroutine::sleep(1); }); ```