在webman中使用swoole一鍵協程化不生效
//webman server.php 配置
return [
'listen' => 'http://0.0.0.0:8787',
'transport' => 'tcp',
'context' => [],
'name' => 'webman',
'count' => cpu_count() * 10,
'user' => '',
'group' => '',
'reusePort' => false,
'event_loop' => Workerman\Events\Swoole::class,
'stop_timeout' => 2,
'pid_file' => runtime_path() . '/webman.pid',
'status_file' => runtime_path() . '/webman.status',
'stdout_file' => runtime_path() . '/logs/stdout.log',
'log_file' => runtime_path() . '/logs/workerman.log',
'max_package_size' => 10 * 1024 * 1024
];
// 測試一鍵協程化
\Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
go(function () {
Db::select("select CONNECTION_ID(),SLEEP(30);");
});
mysql:
SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. (SQL: select CONNECTION_ID(),SLEEP(30);)
redis:
Uncaught Swoole\Error:
Socket#12 has already been bound to another coroutine#25, reading of the same socket in coroutine#29 at t
he same time is not allowed in
webman版本1.4.7
swoole版本5.0.2
php版本8.1
操作系統(tǒng):centos7
用 Channel 實現連接池。
參考:https://gitee.com/mix-php/mix/tree/master/src/object-pool
有人問過這種問題:http://wtbis.cn/q/10007
Socket#12 has already been bound to another coroutine#25, reading of the same socket in coroutine#29
這個報錯說明一鍵協程已經生效了。
報錯的意思是數據庫連接不能同時被多個協程使用,要么每個協程一個數據庫連接,要么使用連接池機制。
協程不是所有composer庫都支持,你要用協程就必須改造所有的第三方composer庫,保證這些庫互斥使用某些資源,保證不會全局變量污染等等。
感謝你的回答!按swoole官網的文檔,一鍵協程化【https://wiki.swoole.com/#/runtime】 說明,我以為可以不需要使用相關連接池。。。