-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swoole 4.2.3 Task中使用协程MySQL报错 #2041
Comments
用go方法创建协程再使用,允许使用但不是自动创建协程。 |
谢谢,明白了。 另外Task中支持协程Runtime吗? |
尝试在Task中开启Runtime,但报错了。 //Task中
\Swoole\Runtime::enableCoroutine();
$pdo = new \PDO('mysql:dbname=test;host=mysql', 'root', 'abc123');
$sql = sprintf('insert into test(name,created_at,updated_at) values("%s","%s","%s")', uniqid(), date('Y-m-d H:i:s'), date('Y-m-d H:i:s'));
$ret = $pdo->exec($sql);
var_dump($ret);
|
@hhxsv5 |
现在搞不清楚,哪些地方是自动创建协程的,哪些需要手动创建。 |
@xiangjihan 根据文档,swoole_server和swoole_http_server将为每一个请求创建对应的协程,可以在onRequet、onReceive、onConnect中使用,其他地方都需手动创建协程, @twose 我的理解是否正确? |
测试了下,在Task中使用启用协程Runtime,第一次执行成功,第二次开始报错。 go(function () {
\Swoole\Runtime::enableCoroutine(); #注释掉这行就正常
$pdo = new \PDO('mysql:dbname=test;host=mysql', 'root', 'abc123');
$sql = sprintf('insert into test(name,created_at,updated_at) values("%s","%s","%s")', uniqid(), date('Y-m-d H:i:s'), date('Y-m-d H:i:s'));
$ret = $pdo->exec($sql);
var_dump($ret);
});
|
@hhxsv5 理解正确, 稍后我添加一个相关单元测试 |
@twose 上面那个报错是怎么回事啊 |
@hhxsv5 断线了需要重新创建连接 |
我是worker和task worker数都置为1,投递了三次任务,任务中go开协程连接数据库执行sql,第二次任务开始就报 |
@hhxsv5 |
😢github 出bug了,贴了N次,贴不上。 |
<?php
$http = new swoole_http_server('127.0.0.1', 5200);
$http->set([
'daemonize' => false,
'worker_num' => 1,
'task_worker_num' => 1,
]);
$http->on('Request', function (swoole_http_request $request, swoole_http_response $response) use ($http) {
$taskId = $http->task('run');
$response->end('Hello LaravelS #' . $taskId);
});
$http->on('Task', function (swoole_http_server $server, $taskId, $srcWorkerId, $data) {
go(function () {
\Swoole\Runtime::enableCoroutine(); #注释掉这行就正常
$pdo = new PDO('mysql:dbname=test;host=127.0.0.1', 'root', 'xy123456');
$sql = sprintf('insert into test(name,created_at,updated_at) values("%s","%s","%s")', uniqid(),
date('Y-m-d H:i:s'), date('Y-m-d H:i:s'));
$pdo->exec($sql);
var_dump($pdo->lastInsertId());
});
});
$http->on('Finish', function (swoole_http_server $server, $taskId, $data) {
});
$http->start();
|
1 similar comment
<?php
$http = new swoole_http_server('127.0.0.1', 5200);
$http->set([
'daemonize' => false,
'worker_num' => 1,
'task_worker_num' => 1,
]);
$http->on('Request', function (swoole_http_request $request, swoole_http_response $response) use ($http) {
$taskId = $http->task('run');
$response->end('Hello LaravelS #' . $taskId);
});
$http->on('Task', function (swoole_http_server $server, $taskId, $srcWorkerId, $data) {
go(function () {
\Swoole\Runtime::enableCoroutine(); #注释掉这行就正常
$pdo = new PDO('mysql:dbname=test;host=127.0.0.1', 'root', 'xy123456');
$sql = sprintf('insert into test(name,created_at,updated_at) values("%s","%s","%s")', uniqid(),
date('Y-m-d H:i:s'), date('Y-m-d H:i:s'));
$pdo->exec($sql);
var_dump($pdo->lastInsertId());
});
});
$http->on('Finish', function (swoole_http_server $server, $taskId, $data) {
});
$http->start();
|
确认是PHP71的PDO的问题, PHP72无此问题, 待追踪 |
你的MySQL版本是? |
github还没恢复... |
@twose |
@twose 好的,需要新开个issue吗? |
Please answer these questions before submitting your issue. Thanks!
Swoole 4.2.3 Task中使用协程MySQL报错
What did you expect to see?
正常执行协程MySQL
What did you see instead?
What version of Swoole are you using (show your
php --ri swoole
)?4.2.3
What is your machine environment used (including version of kernel & php & gcc) ?
Debian GNU/Linux 8 \n \l
PHP 7.1.21 (cli) (built: Sep 7 2018 22:17:18) ( NTS )
gcc version 4.9.2 (Debian 4.9.2-10+deb8u1)
The text was updated successfully, but these errors were encountered: