zoukankan      html  css  js  c++  java
  • swoft 使用协程 初试

    控制器访问 /hi

    /**
    	 * @SwoftBeanAnnotationMappingInject("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 AppService;
    use function foofunc;
    use SwoftBeanAnnotationMappingBean;
    use SwoftCo;
    use SwoftLogHelperCLog;
    
    
    /**
     * Class UserService
     * @Bean("UserService")
     * @package AppService
     */
    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] AppServiceUserService:AppService{closure}(28) hello
    2020/07/20-20:22:49 [INFO] AppServiceUserService:AppService{closure}(28) hello
    2020/07/20-20:22:49 [INFO] AppServiceUserService:AppService{closure}(28) hello
    2020/07/20-20:22:49 [INFO] AppServiceUserService:AppService{closure}(28) hello
    2020/07/20-20:22:49 [INFO] AppServiceUserService:AppService{closure}(28) hello
    2020/07/20-20:22:49 [INFO] AppServiceUserService:AppService{closure}(28) hello
    2020/07/20-20:22:49 [INFO] AppServiceUserService:AppService{closure}(28) hello
    2020/07/20-20:22:49 [INFO] AppServiceUserService:AppService{closure}(28) hello
    2020/07/20-20:22:49 [INFO] AppServiceUserService:AppService{closure}(28) hello
    2020/07/20-20:22:49 [INFO] AppServiceUserService:AppService{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";
    }
  • 相关阅读:
    shell getopt getopts获取参数
    apache+svn+ladp认证
    SVN 迁移项目分支
    iptables 优先级
    很实用的一篇HTTP状态码
    套路还在——矩阵计算估值
    CU上看到的一个简单的算法帖子
    linux下服务端实现公网数据转发
    c++接口实现与分离(转载)
    c++继承概念
  • 原文地址:https://www.cnblogs.com/brady-wang/p/13347515.html
Copyright © 2011-2022 走看看