由于协程的不受控制性,业务开发中使用到协程,需要统一规范。
经项目组成员达成一致,且经过多次测试使用,采用如下的一套规范代码。
严格设置phpstom模版代码:
复制粘贴以下模版代码:
$chan = new \app\common\util\MmChannel(1);//基本原则:管道长度 = go()协程数量
$CoroutineContext = get_thread_context();
go(function () use ($CoroutineContext,$chan) {
try {
set_thread_context($CoroutineContext);//* 接受主协程上下文数据
//----------------logic code 逻辑代码--------------
//$chan->push();
} catch (\Throwable $e) {//异常处理
get_thread_context('PtLog')->recordErrorCode(10014, $e->getMessage());
}
});
效果: