日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

swoole 协程channel乱测

發(fā)布時(shí)間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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連接池
class redispool {protected $mypool;public function __construct($size){$this->mypool = new Swoole\Coroutine\Channel($size);for ($i=0;$i<$size;$i++){$redis = new Swoole\Coroutine\Redis();$redis->setOptions(['compatibility_mode' => true]);$rconn = $redis->connect('127.0.0.1',6379);if ($rconn == false){throw new \http\Exception\RuntimeException('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();} }

?

轉(zhuǎn)載于:https://www.cnblogs.com/saintdingspage/p/11276399.html

總結(jié)

以上是生活随笔為你收集整理的swoole 协程channel乱测的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。