swoft 使用协程 初试
协程 初试 使用
2023-09-14 09:12:10 时间
控制器访问 /hi
/** * @Swoft\Bean\Annotation\Mapping\Inject("UserService") * @var UserService */ public $userService; /** * @RequestMapping("/") * @throws Throwable */ public function index(): Response { /** @var Renderer $renderer */ $renderer = Swoft::getBean('view'); $content = $renderer->render('home/index'); return context()->getResponse()->withContentType(ContentType::HTML)->withContent($content); } /** * @RequestMapping("/hi") * * @return Response */ public function hi() { return $this->userService->test(); }
调用service 里面使用协程 最终访问前端页面 立马返回了 数据
UserService.php
<?php namespace App\Service; use function foo\func; use Swoft\Bean\Annotation\Mapping\Bean; use Swoft\Co; use Swoft\Log\Helper\CLog; /** * Class UserService * @Bean("UserService") * @package App\Service */ class UserService { public function __construct() { } public function test() { Co::create(function(){ Co::sleep(10); for($i=0;$i<10;$i++){ CLog::info("hello"); } }); Co::create(function(){ Co::sleep(20); var_dump("world"); }); $id = Co::id(); var_dump($id); $id = Co::tid(); var_dump($id); return "nihao"; } }
过了十秒 和二十秒后分别打印出了数据
erver start success (Master PID: 18085, Manager PID: 18090) int(2) int(2) 2020/07/20-20:22:49 [INFO] App\Service\UserService:App\Service\{closure}(28) hello 2020/07/20-20:22:49 [INFO] App\Service\UserService:App\Service\{closure}(28) hello 2020/07/20-20:22:49 [INFO] App\Service\UserService:App\Service\{closure}(28) hello 2020/07/20-20:22:49 [INFO] App\Service\UserService:App\Service\{closure}(28) hello 2020/07/20-20:22:49 [INFO] App\Service\UserService:App\Service\{closure}(28) hello 2020/07/20-20:22:49 [INFO] App\Service\UserService:App\Service\{closure}(28) hello 2020/07/20-20:22:49 [INFO] App\Service\UserService:App\Service\{closure}(28) hello 2020/07/20-20:22:49 [INFO] App\Service\UserService:App\Service\{closure}(28) hello 2020/07/20-20:22:49 [INFO] App\Service\UserService:App\Service\{closure}(28) hello 2020/07/20-20:22:49 [INFO] App\Service\UserService:App\Service\{closure}(28) hello string(5) "world"
改为帮助函数
sgo(function(){
Co::sleep(10);
for($i=0;$i<10;$i++){
CLog::info("hello");
}
});
srun
启动协程并等待执行结束。
public function test()
{
srun(function(){
sgo(function(){
for($i=0;$i<10;$i++){
Co::sleep(1);
CLog::info("hello");
}
});
sgo(function(){
for($i=0;$i<10;$i++){
Co::sleep(1);
CLog::warning("hello");
}
});
return true;
});
$id = Co::id();
var_dump($id);
$id = Co::tid();
var_dump($id);
return "nihao";
}
相关文章
- 进程、线程与协程
- 第五十七节,线程、进程、协程
- Kotlin 朱涛-18 实战 Retrofit 协程 挂起函数 异步回调
- Kotlin 朱涛-13 协程 思维模型 非阻塞 coroutine 线程
- Golang协程案例:并行计算指定数的阶乘(未使用管道)
- [Sw] Swoole-4.2.9 可以尝试愉快应用 Swoole 协程
- 0-1Python协程演变之路
- Python黑魔法 --- 异步IO( asyncio) 协程
- 【Kotlin 协程】Flow 流异常处理 ( 收集元素异常处理 | 使用 try...catch 代码块捕获处理异常 | 发射元素时异常处理 | 使用 Flow#catch 函数捕获处理异常 )
- 【Kotlin 协程】协程异常处理 ③ ( 协程异常处理器 CoroutineExceptionHandler 捕获异常 | 验证 CoroutineScope 协程的异常捕捉示例 )
- 【Kotlin 协程】协程异常处理 ② ( SupervisorJob 协程 | supervisorScope 协程作用域构建器函数 )
- python里仅收集所有协程执行结果
- swoole深入学习 8. 协程 转