控制器访问 /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";
}

最新文章

  1. LCS记录
  2. 项目公共js(vue.js)
  3. Mysql 中 iddata1的缩小步骤
  4. C# 使用Trace记录程序日志
  5. (转)jQuery Mobile 移动开发中的日期插件Mobiscroll 2.3 使用说明
  6. MFC9.0 Outlook控件的标题显示无法修改
  7. linux 命令之系统活动报告sar
  8. [.ashx檔?泛型处理例程?]基础入门#3....ADO.NET 与 将DB里面的二进制图片还原
  9. 64 位系统遇到未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0
  10. VB最新使用教程
  11. redis安装与使用
  12. Codeforces 466 E. Information Graph
  13. Total Command help
  14. AndroidStudio3.0无法打开Android Device Monitor的解决办法(An error has occurred on Android Device Monitor)
  15. pandas的一些
  16. IdentityServer4客户端JWT解密实现(基于.net4.0)
  17. JS学习之路之JavaScript match() 方法
  18. 关于vue2.0 cnpm 镜像安装
  19. 自然语言处理NLP-云端API汇总
  20. 20165215 实验一 Java开发环境的熟悉

热门文章

  1. 从一次编译出发梳理概念: Jetty,Jersey,hk2,glassFish,Javax,Jakarta
  2. 最火的开源 IDE介绍与安装教程
  3. 代码检查工具 Sonar 安装&amp;使用
  4. 一分钟理解Redo Undo
  5. win10病毒和威胁防护无法重新启动解决方法
  6. 微服务实战系列(七)-网关springcloud gateway
  7. 1-浅谈 python变量
  8. P4715 【深基16.例1】淘汰赛
  9. 大话Python函数底层逻辑
  10. Java知识系统回顾整理01基础06数组01创建数组