首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
基于swoole协程客户端不发生协程切换的问题
### 问题描述 ### Swoole版本,PHP版本,以及操作系统版本信息 php版本: (base) [root@localhost php]# php -v PHP 7.4.15 (cli) (built: Feb 9 2021 17:51:20) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies swoole 版本: (base) [root@localhost php]# php --ri swoole swoole Swoole => enabled Author => Swoole Team <team@swoole.com> Version => 4.6.4-dev Built => Feb 9 2021 18:22:54 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 json => enabled curl-native => 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 linux 版本: (base) [root@localhost php]# cat /proc/version Linux version 3.10.0-514.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Tue Nov 22 16:42:41 UTC 2016 ### 相关代码 ```php # server端 192.168.1.6/swoole/index.php的代码 $t = "Guest"; if (isset($_GET['t'])) { sleep(3); $t = $_GET['t']; } $html = <<<data this is my server, user is :$t; data; exit($html); # 协程http客户端代码 $cid1 = go(function(){ $client = new httpClient('192.168.1.6', 80); $client->get('/swoole/index.php?t=justin'); echo $client->body.PHP_EOL; $client->close(); }); $cid2 = go(function(){ $client = new httpClient('192.168.1.6', 80); $client->get('/swoole/index.php'); echo $client->body.PHP_EOL; $client->close(); }); var_dump($cid1,$cid2); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 期望结果: 先打印 78 行var_dump($cid1,$cid2); 在打印 73 行$cid2 中的 echo $client->body.PHP_EOL; 再打印 67 行$cid1 Z中 echo $client->body.PHP_EOL; 实际 1,78行 var_dump($cid1,$cid2); 2,67行 $cid1 Z中 echo $client->body.PHP_EOL; 3. 73行 $cid2 中的 echo $client->body.PHP_EOL; 存在入参t=justin的时候,发送http请求,会在服务端sleep(3),应该发生协程调度 走 cid2 的 http请求啊。 为什么没有切换呢
发布于3年前 · 5 次浏览 · 来自
提问
justingjm
### 问题描述 ### Swoole版本,PHP版本,以及操作系统版本信息 php版本: (base) [root@localhost php]# php -v PHP 7.4.15 (cli) (built: Feb 9 2021 17:51:20) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies swoole 版本: (base) [root@localhost php]# php --ri swoole swoole Swoole => enabled Author => Swoole Team <team@swoole.com> Version => 4.6.4-dev Built => Feb 9 2021 18:22:54 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 json => enabled curl-native => 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 linux 版本: (base) [root@localhost php]# cat /proc/version Linux version 3.10.0-514.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Tue Nov 22 16:42:41 UTC 2016 ### 相关代码 ```php # server端 192.168.1.6/swoole/index.php的代码 $t = "Guest"; if (isset($_GET['t'])) { sleep(3); $t = $_GET['t']; } $html = <<<data this is my server, user is :$t; data; exit($html); # 协程http客户端代码 $cid1 = go(function(){ $client = new httpClient('192.168.1.6', 80); $client->get('/swoole/index.php?t=justin'); echo $client->body.PHP_EOL; $client->close(); }); $cid2 = go(function(){ $client = new httpClient('192.168.1.6', 80); $client->get('/swoole/index.php'); echo $client->body.PHP_EOL; $client->close(); }); var_dump($cid1,$cid2); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 期望结果: 先打印 78 行var_dump($cid1,$cid2); 在打印 73 行$cid2 中的 echo $client->body.PHP_EOL; 再打印 67 行$cid1 Z中 echo $client->body.PHP_EOL; 实际 1,78行 var_dump($cid1,$cid2); 2,67行 $cid1 Z中 echo $client->body.PHP_EOL; 3. 73行 $cid2 中的 echo $client->body.PHP_EOL; 存在入参t=justin的时候,发送http请求,会在服务端sleep(3),应该发生协程调度 走 cid2 的 http请求啊。 为什么没有切换呢
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2021-02-17
鲁飞
测试正常 ``` int(1) int(2) this is my server, user is :Guest; this is my server, user is :justin; ``` ```php $http = new Swoole\Http\Server('0.0.0.0', 9501); //$http->set(['hook_flags' => false, 'enable_coroutine' => false]); $http->on('Request', function ($request, $response) { $t = "Guest"; if (isset($request->get['t'])) { sleep(3); $t = $request->get['t']; } $html = <<<data this is my server, user is :$t; data; $response->header('Content-Type', 'text/html; charset=utf-8'); $response->end($html); }); $http->start(); use Swoole\Coroutine\Http\Client; use Swoole\Event; $cid1 = go(function(){ $client = new Client('127.0.0.1', 9501); $client->get('/?t=justin'); echo $client->body.PHP_EOL; $client->close(); }); $cid2 = go(function(){ $client = new Client('127.0.0.1', 9501); $client->get('/'); echo $client->body.PHP_EOL; $client->close(); }); var_dump($cid1, $cid2); Event::wait(); ```
赞
0
回复
2021-02-17
justingjm
感谢回复,应该是我的server 端有问题, 一次只能承接一个请求,上一个没有返回之前,下一个必须等待. 用的window下的 phpstudy
赞
0
回复
微信公众号
热门内容
作者其它话题
- Process::signal 在mac下不能被触发
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。