首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
听说协程很牛逼,用thinkphp框架尝试协程研究,但是一直卡死出不来结果
### 问题描述 听说协程很牛逼,用thinkphp框架尝试协程研究,但是一直卡死出不来结果,不知道是我太菜了还是swoole太高端了,经过半天折腾连个协程的用例都搞不出来,特此向高手们请教 ### Swoole版本,PHP版本,以及操作系统版本信息 ThinkPHP 6.0.8 PHP版本 7.3.15 MySQL版本 8.0.13 当前操作系统 Linux CentOS 7.7 64位 swoole Swoole => enabled Author => Swoole Team <team@swoole.com> Version => 4.8.0 Built => Oct 14 2021 18:42:09 coroutine => enabled with boost asm context epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled openssl => OpenSSL 1.0.2k-fips 26 Jan 2017 http2 => enabled pcre => enabled zlib => 1.2.7 mutex_timedlock => enabled pthread_barrier => enabled futex => enabled async_redis => enabled Directive => Local Value => Master Value swoole.enable_coroutine => On => On swoole.enable_library => On => On swoole.enable_preemptive_scheduler => Off => Off swoole.display_errors => On => On swoole.use_shortname => On => On swoole.unixsock_buffer_size => 8388608 => 8388608 ### 相关代码 ```php public function work() { return Db::query("select SLEEP(0.1)"); } public function hello($name = 'ThinkPHP6') { \Swoole\Runtime::enableCoroutine(); // 开启一键协程化 $time = microtime(true); for($i = 0; $i < 3; ++$i) { $this->work(); } echo 'time: ', (microtime(true) - $time), 's', PHP_EOL; $time = microtime(true); for($i = 0; $i < 3; ++$i) { go(function(){ $this->work(); }); } swoole_event_wait(); // 等待所有协程执行完 echo 'time: ', (microtime(true) - $time), 's', PHP_EOL; } ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 我期待看到协程对大量数据库读写方面带来的性能提升,实际上是一直卡死,出不来结果,只要把协程相关的代码 go, swoole_event_wait注释就正常了
发布于4年前 · 88 次浏览 · 来自
提问
PHP初学者9
### 问题描述 听说协程很牛逼,用thinkphp框架尝试协程研究,但是一直卡死出不来结果,不知道是我太菜了还是swoole太高端了,经过半天折腾连个协程的用例都搞不出来,特此向高手们请教 ### Swoole版本,PHP版本,以及操作系统版本信息 ThinkPHP 6.0.8 PHP版本 7.3.15 MySQL版本 8.0.13 当前操作系统 Linux CentOS 7.7 64位 swoole Swoole => enabled Author => Swoole Team <team@swoole.com> Version => 4.8.0 Built => Oct 14 2021 18:42:09 coroutine => enabled with boost asm context epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled openssl => OpenSSL 1.0.2k-fips 26 Jan 2017 http2 => enabled pcre => enabled zlib => 1.2.7 mutex_timedlock => enabled pthread_barrier => enabled futex => enabled async_redis => enabled Directive => Local Value => Master Value swoole.enable_coroutine => On => On swoole.enable_library => On => On swoole.enable_preemptive_scheduler => Off => Off swoole.display_errors => On => On swoole.use_shortname => On => On swoole.unixsock_buffer_size => 8388608 => 8388608 ### 相关代码 ```php public function work() { return Db::query("select SLEEP(0.1)"); } public function hello($name = 'ThinkPHP6') { \Swoole\Runtime::enableCoroutine(); // 开启一键协程化 $time = microtime(true); for($i = 0; $i < 3; ++$i) { $this->work(); } echo 'time: ', (microtime(true) - $time), 's', PHP_EOL; $time = microtime(true); for($i = 0; $i < 3; ++$i) { go(function(){ $this->work(); }); } swoole_event_wait(); // 等待所有协程执行完 echo 'time: ', (microtime(true) - $time), 's', PHP_EOL; } ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 我期待看到协程对大量数据库读写方面带来的性能提升,实际上是一直卡死,出不来结果,只要把协程相关的代码 go, swoole_event_wait注释就正常了
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2021-10-16
鲁飞
首先 thinkphp 并不是一个协程框架,需要安装 think-swoole 组件来使用 swoole 的能力。 你可以写个单文件来测试,或者使用其他协程框架。
赞
0
回复
2021-10-16
PHP初学者9
大哥,这个我还是知道的,没有那么白。。。 think-swoole版本是 think-swoole3,目前go是能异步,但是无法拿到所有协程执行后的结果,关键就在 swoole_event_wait(); 这个函数,会阻塞,并且导致程序一直等待
赞
0
回复
2021-10-20
鲁飞
回复
PHP初学者9
你要拿结果就考虑其他的api,waitgroup之类的。
赞
0
回复
2021-10-22
Rango
使用 `Coroutine::join()`、`WaitGroup`、`Coroutine\Barrier`、`Channel` 等协程同步工具
赞
0
回复
2021-11-07
李铭昕
你这个代码很显然会报错,在 tp 里面,如果想要并发执行,你需要 new 多个 db connection 而你并发使用一个,会有以下错误 ``` PHP Fatal error: Uncaught Swoole\Error: Socket#12 has already been bound to another coroutine#8, reading of the same socket in coroutine#6 at the same time is not allowed ``` 所以,如果你的情况是卡主,那应该是哪个地方有问题。你可以使用原生 PDO 进行测试,但需要协程里执行时,直接 new PDO。 最后,不建议在 FPM 框架里跑 协程。。。
赞
0
回复
2021-11-25
_+
tp的swoole源码里,我看是通过进程池管理的协程服务端,这个写法是不是相当于写了一个子协程,在子协程里应该将tp中的容器和框架的一些配置放到子协程里吧,不能共用吧
赞
0
回复