swoole 协程channel乱测
生活随笔
收集整理的這篇文章主要介紹了
swoole 协程channel乱测
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
channel和數(shù)組差不多,可以被用作隊(duì)列,屬性capacity是設(shè)置容量,isEmpty() isFull() 用來判斷隊(duì)列是空還是滿,push()加入隊(duì)列 pop()彈出隊(duì)列
interface pusher {function push($data); } #require 'redisconn.php'; class mypusher implements pusher {protected $mychannel;protected $size=10;public function __construct(){$this->mychannel = new Swoole\Coroutine\Channel(10);$this->mylen = $this->mychannel->length();echo "容量".$this->mychannel->capacity.PHP_EOL;}public function push($data){if ($this->mychannel->isFull()){return 0;}else{//發(fā)送通知$this->mychannel->push($data);echo "當(dāng)前元素個(gè)數(shù)".$this->mychannel->length();return 1;}}public function pop(){if($this->mychannel->isEmpty()){return 0;}else{echo "當(dāng)前元素個(gè)數(shù)".$this->mychannel->length().PHP_EOL;var_dump($this->mychannel->pop());echo PHP_EOL."還剩元素個(gè)數(shù)".$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();});測試結(jié)果:
容量10 當(dāng)前元素個(gè)數(shù)1當(dāng)前元素個(gè)數(shù)2當(dāng)前元素個(gè)數(shù)3當(dāng)前元素個(gè)數(shù)3 array(2) {["name"]=>string(2) "cj"["age"]=>int(20) }還剩元素個(gè)數(shù)2 當(dāng)前元素個(gè)數(shù)2 array(2) {["name"]=>string(3) "cpc"["age"]=>int(22) }還剩元素個(gè)數(shù)1 當(dāng)前元素個(gè)數(shù)1 array(2) {["name"]=>string(3) "xxd"["age"]=>int(40) }還剩元素個(gè)數(shù)0- 實(shí)現(xiàn)一個(gè)redis連接池
?
轉(zhuǎn)載于:https://www.cnblogs.com/saintdingspage/p/11276399.html
總結(jié)
以上是生活随笔為你收集整理的swoole 协程channel乱测的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在数据中心空间进行测试
- 下一篇: Node.js(爱前端) 一