遇到的问题
我想在服务启动前,先链接到各个服务(例如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();
使用 chan 实现连接池。一个连接对象只能用于一个协程