这里写问题描述
我使用协程事件驱动时,访问8787的index页面主进程并未直接返回response的结果,而是等待协程进程执行完毕后才返回结果
public function index(Request $request)
{
Coroutine::create(function(){
Timer::sleep(5);
echo "hello coroutine\n";
});
$responseData = new ResponseData();
$resDataArr = $responseData->ResponseData(201,array(
"age" => 111,
"name" => "李四"
),"success",[
"Content-Type"=>"application/json",
]);
return $resDataArr;
}
webman是基于workerman开发的,所以webman可以使用workerman的协程特性。
协程支持Swoole Swow和Fiber三种驱动。
前提条件
PHP >= 8.1
Workerman >= 5.1.0 (composer require workerman/workerman ~v5.1)
webman-framework >= 2.1 (composer require workerman/webman-framework ~v2.1)
安装了swoole或者swow扩展,或者安装了composer require revolt/event-loop (Fiber)
协程默认是关闭的,需要单独设置eventLoop开启
开启方法
webman支持为不同的进程开启不同的驱动,所以你可以在config/process.php中通过eventLoop配置协程驱动:
文档地址https://www.workerman.net/doc/webman/coroutine/coroutine.html
测试了,没问题,立刻返回ok,5秒后终端打印 hello coroutine。
要么没配置好event-loop,要么代码ResponseData什么的有问题。