zoukankan      html  css  js  c++  java
  • swoole 协程channel乱测

    channel和数组差不多,可以被用作队列,属性capacity是设置容量,isEmpty() isFull() 用来判断队列是空还是满,push()加入队列 pop()弹出队列

    interface pusher
    {
        function push($data);
    }
    #require 'redisconn.php';
    class mypusher implements pusher
    {
        protected $mychannel;
        protected $size=10;
        public function __construct()
        {
            $this->mychannel = new SwooleCoroutineChannel(10);
            $this->mylen = $this->mychannel->length();
            echo "容量".$this->mychannel->capacity.PHP_EOL;
        }
    
        public function push($data)
        {
            if ($this->mychannel->isFull())
            {
                return 0;
            }
            else{
                //发送通知
                $this->mychannel->push($data);
                echo "当前元素个数".$this->mychannel->length();
                return 1;
            }
        }
    
        public function pop()
        {
            if($this->mychannel->isEmpty())
            {
                return 0;
            }
            else
            {
                echo "当前元素个数".$this->mychannel->length().PHP_EOL;
                var_dump($this->mychannel->pop());
                echo PHP_EOL."还剩元素个数".$this->mychannel->length().PHP_EOL;
                return 1;
            }
        }
    }
    go(function (){
        $thispusher = new mypusher();
        $thispusher->push(['name'=>'cj','age'=>20]);
        $thispusher->push(['name'=>'cpc','age'=>22]);
        $thispusher->push(['name'=>'xxd','age'=>40]);
        $thispusher->pop();
        $thispusher->pop();
        $thispusher->pop();
    
    });

    测试结果:

    容量10
    当前元素个数1当前元素个数2当前元素个数3当前元素个数3
    array(2) {
      ["name"]=>
      string(2) "cj"
      ["age"]=>
      int(20)
    }
    
    还剩元素个数2
    当前元素个数2
    array(2) {
      ["name"]=>
      string(3) "cpc"
      ["age"]=>
      int(22)
    }
    
    还剩元素个数1
    当前元素个数1
    array(2) {
      ["name"]=>
      string(3) "xxd"
      ["age"]=>
      int(40)
    }
    
    还剩元素个数0
    • 实现一个redis连接池
    class redispool
    {
        protected $mypool;
        public function __construct($size)
        {
            $this->mypool = new SwooleCoroutineChannel($size);
            for ($i=0;$i<$size;$i++)
            {
                $redis = new SwooleCoroutineRedis();
                $redis->setOptions(['compatibility_mode' => true]);
                $rconn = $redis->connect('127.0.0.1',6379);
                if ($rconn == false)
                {
                    throw new httpExceptionRuntimeException('Damn~ fail to connet redis-server');
                }
                else{
                    $this->mypool->push($redis);
                }
            }
        }
    
        public function push($redis)
        {
            $this->mypool->push($redis);
        }
        public function pop()
        {
            return $this->mypool->pop();
        }
    }
  • 相关阅读:
    软件开发与定制报价
    C# HttpHelper 1.0正式版发布
    C#仿QQ皮肤-TextBox 控件实现
    HTML5学习笔记第一节(智能提示和视频音频标签)
    C#多线程|匿名委托传参数|测试您的网站能承受的压力|附源代码升级版
    JavascriptHelp
    我的个人博客论坛版建立啦!
    Win7 + VirtualBox安装Mac OS X雪豹操作系统图文详解
    Sql2005性能工具(SQL Server Profiler和数据库引擎优化顾问)使用方法详解
    Webcast 系列课程 NET最全,最权威的学习资源
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/11276399.html
Copyright © 2011-2022 走看看