mysql连接池报错client has already been bound to another coroutine?

看了swoole的协成文档,说在不同协成内不能用通一个client,但是做了一个简单连接池还是会报mysql client has already been bound to another coroutine!不知道是我哪里写的不对,或者没懂意思,请大神指教.

mysqlPool.php ------------------------------

<?php
/**
 * Created by PhpStorm.
 * User: shenyang.qiu
 * Date: 2019/5/6
 * Time: 16:27
 */
class mysqlPool {
    const  MAXNUM = 50;
    static $nownum = 0;
    static $pool = [];
    public static function getMysql()
    {
        if (count(self::$pool)) {
            return array_pop(self::$pool);
        } else {
            try {
                if (self::$nownum >self::MAXNUM) {
                    $timeout = 3;
                    while ($timeout) {
                        co::sleep(0.5);
                        $timeout = $timeout -0.5;
                        if(count(self::$pool)) {
                            return array_pop(self::$pool);
                        }
                    }
                    return false;
                } else {
                    $mysql = new \Swoole\Coroutine\MySQL();
                    self::$nownum++;
                    $mysql->connect([
                        'host' => '******',
                        'port'=>'******',
                        'user' => '****',
                        'password' => '*****',
                        'database' => '****',
                    ]);
                    array_push(self::$pool,$mysql);

                    return $mysql;
                }
            } catch (swoole_mysql_exception $exception) {
                var_dump($exception->getMessage());
                return false;
            }

        }
    }


    public static function realseMysql($pdo)
    {
        return array_push(self::$pool,$pdo);
    }
}

server.php ------------------------------------

<?php
/**
 * Created by PhpStorm.
 * User: shenyang.qiu
 * Date: 2019/5/7
 * Time: 13:16
 */
require_once 'mysqlPool.php';
$server = new swoole_http_server('0.0.0.0',9502,SWOOLE_BASE,SWOOLE_TCP);
$server->on('workerstart',function(swoole_http_server $server){
    swoole_timer_tick(2000,function(){
        var_dump(mysqlPool::$nownum);
    });
});
$server->on('request',function(swoole_http_request $request, swoole_http_response $response) {
    try {
        $mysql =  mysqlPool::getMysql();
        if ($mysql) {
            $mysql->setDefer();
            $mysql->query('select * from admin');
            $redis = new \Swoole\Coroutine\Redis();
            $redis->connect('127.0.0.1',6379);
            $redis->setDefer();
            $redis->get('test');
            $mysqlinfo = $mysql->recv(1);
            $redisinfo = $redis->recv(1);
            mysqlPool::realseMysql($mysql);
            unset($mysql);
            $redis->close();
            $response->end(json_encode($mysqlinfo));
        } else {
            $response->end('get mysql filed');
            var_dump('get mysql failed');
        }
    } catch (Exception $e)  {
        var_dump('有错误了');
    }



});
$server->start();

client.php ----------------------------

go(function() {
    $t1 = microtime(true);
    $client = [];
    for ($i=0;$i<100;$i++) {
        $httpclient = new \Swoole\Coroutine\Http\Client('127.0.0.1',9502);
        $httpclient->set([
            'Host' => "localhost",
            "User-Agent" => 'Chrome/49.0.2587.3',
            'Accept' => 'text/html,application/xhtml+xml,application/xml',
            'Accept-Encoding' => 'gzip',
        ]);
        $client[] = $httpclient;
        $httpclient->setDefer();
        $httpclient->get('/');

    }
    foreach ($client as $row) {
        $msg = $row->recv();
        var_dump($row->body);
        $row->close();
    }
    $t2 = microtime(true);
    var_dump($t2-$t1);

});

报错信息:
[2019-05-08 14:48:53 *21631.0] ERROR check_bind (ERROR 10002): mysql client has already been bound to another coroutine#57, reading or writing of the same socket in multiple coroutines at the same time is not allowed.

阅读 4.7k
1 个回答

写的有问题,一个连接同时在两个连接使用 就好出现这个错误

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进