作者:wu勿望我_957 | 来源:互联网 | 2023-06-29 16:30
看了swoole的协成文档,说在不同协成内不能用通一个client,但是做了一个简单连接池还是会报mysqlclienthasalreadybeenboundtoanotherc
看了swoole的协成文档,说在不同协成内不能用通一个client,但是做了一个简单连接池还是会报mysql client has already been bound to another coroutine!不知道是我哪里写的不对,或者没懂意思,请大神指教.
mysqlPool.php ------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| /**
* 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 ------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| /**
* 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 ----------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 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.