這里寫問題描述
我使用協(xié)程事件驅(qū)動時,訪問8787的index頁面主進(jìn)程并未直接返回response的結(jié)果,而是等待協(xié)程進(jìn)程執(zhí)行完畢后才返回結(jié)果
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開發(fā)的,所以webman可以使用workerman的協(xié)程特性。
協(xié)程支持Swoole Swow和Fiber三種驅(qū)動。
前提條件
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擴(kuò)展,或者安裝了composer require revolt/event-loop (Fiber)
協(xié)程默認(rèn)是關(guān)閉的,需要單獨(dú)設(shè)置eventLoop開啟
開啟方法
webman支持為不同的進(jìn)程開啟不同的驅(qū)動,所以你可以在config/process.php中通過eventLoop配置協(xié)程驅(qū)動:
文檔地址http://wtbis.cn/doc/webman/coroutine/coroutine.html
測試了,沒問題,立刻返回ok,5秒后終端打印 hello coroutine。
<?php
namespace app\controller;
use support\Request;
use Workerman\Coroutine;
use Workerman\Timer;
class IndexController
{
public function index(Request $request)
{
Coroutine::create(function(){
Timer::sleep(5);
echo "hello coroutine\n";
});
return 'ok';
}
}
要么沒配置好event-loop,要么代碼ResponseData什么的有問題。