首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
while(ture)里面的go(function())里面的协程是会一直被堵塞的吗
### 问题描述 while(ture)里面的go(function())里面的协程是会一直被堵塞的吗,像如下代码,执行到go(function())里面的fwrite之后,后面的代码就一直没有返回了。 ### Swoole版本,PHP版本,以及操作系统版本信息 swoole-4.5.1 php-7.3 mac-10.15.2 ### 相关代码 ```php <?php Co\run(function () { while(true) { $file = fopen("php://stdin", "r"); $line = fgets($file); fclose($file); $myfile = fopen("/tmp/newfile.txt", "a") or die("Unable to open file!"); $txt = "begin\n"; fwrite($myfile, $txt); //Co::set(['hook_flags' => SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL]); go(function() use ($line, $myfile) { $txt = "now go\n"; fwrite($myfile, $txt); //Co::sleep(2.0); $cli = new Swoole\Coroutine\Http\Client('baidu.com', 80); $cli->get('/'); $cli->body; //echo "\n"; $cli->close(); $stinarray = explode(" ", $line); $account = $stinarray[0]; $result = $account." OK\n"; print $result; $txt = $result; fwrite($myfile, $txt); fclose($myfile); }); } }); ?> ``` ### 你期待的结果是什么?实际看到的错误信息又是什么?
发布于6年前 · 6 次浏览 · 来自
提问
j
janjan
### 问题描述 while(ture)里面的go(function())里面的协程是会一直被堵塞的吗,像如下代码,执行到go(function())里面的fwrite之后,后面的代码就一直没有返回了。 ### Swoole版本,PHP版本,以及操作系统版本信息 swoole-4.5.1 php-7.3 mac-10.15.2 ### 相关代码 ```php <?php Co\run(function () { while(true) { $file = fopen("php://stdin", "r"); $line = fgets($file); fclose($file); $myfile = fopen("/tmp/newfile.txt", "a") or die("Unable to open file!"); $txt = "begin\n"; fwrite($myfile, $txt); //Co::set(['hook_flags' => SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL]); go(function() use ($line, $myfile) { $txt = "now go\n"; fwrite($myfile, $txt); //Co::sleep(2.0); $cli = new Swoole\Coroutine\Http\Client('baidu.com', 80); $cli->get('/'); $cli->body; //echo "\n"; $cli->close(); $stinarray = explode(" ", $line); $account = $stinarray[0]; $result = $account." OK\n"; print $result; $txt = $result; fwrite($myfile, $txt); fclose($myfile); }); } }); ?> ``` ### 你期待的结果是什么?实际看到的错误信息又是什么?
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2020-05-26
郭新华
请看下`一键协程化`的文档,`Co::set(['hook_flags' => SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL]);`放到最上面,我测试没问题。
赞
0
回复
2020-05-26
j
janjan
回复
郭新华
谢谢。我试过了,还是不可以,诡异
赞
0
回复
2020-05-27
qzzm
while(true){ //your code . . //加这样一句在后面也可以解决阻塞 \co::sleep(0.01); }
赞
0
回复