首页
文档
问答
捐赠
源代码
商业产品
Swoole Tracker
Swoole Plus
Swoole Compiler
Swoole 微课程
商业合作
登录
注册
EN
全部
提问
分享
讨论
建议
公告
开发框架
发表新帖
请问 协程初探里面的demo如何才能跑起来,比如下面这个,谢谢
咸鱼大叔
去年
Swoole\Runtime::enableCoroutine();//此行代码后,文件操作,sleep,Mysqli,PDO,streams等都变成异步IO,见'一键协程化'章节 $s = microtime(true); //Co/run()见'协程容器'章节 Co\run(function() { // i just want to sleep... for ($c = 100; $c--;) { go(function () { for ($n = 100; $n--;) { usleep(1000); } }); } }); echo 'use ' . (microtime(true) - $s) . ' s';
评论
2020-03-17
郭新华
用server的模式跑,会自动创建协程容器,不用co run 看下这个吧 https://wiki.swoole.com/#/coroutine/scheduler
点赞
1
回复
2020-03-17
鲁飞
写入php文件,cli执行 ```php php file.php ```
点赞
0
回复
2020-03-17
咸鱼大叔
回复
鲁飞
<?php $http = new Swoole\Http\Server("0.0.0.0", 9502); $http->on('request', function ($request, $response) { Swoole\Runtime::enableCoroutine(); $s = microtime(true); Co\run(function() { for ($c = 100; $c--;) { go(function () use ($c) { $tmp_filename = "/data/wwwroot/swoole/abc/test-{$c}.php"; for ($n = 100; $n--;) { $self = "test"; file_put_contents($tmp_filename, $self); assert(file_get_contents($tmp_filename) === $self); } unlink($tmp_filename); }); } }); echo 'use ' . (microtime(true) - $s) . ' s'; }); $http->start(); 报错 Swoole\Coroutine\Scheduler::start(): eventLoop has already been created
点赞
0
回复
2020-03-17
Lucifers
回复
咸鱼大叔
是不是已经启动服务了 先ps -ef|grep 文件 查查看有没有已经启动的服务
点赞
0
回复
2020-03-17
Lucifers
cli 模式 先看看文档
点赞
0
回复