## Coroutine\\Channel
通道,類似于`go`語言的`chan`,支持多生產者協程和多消費者協程。底層自動實現了協程的切換和調度。 通道與`PHP`的`Array`類似,僅占用內存,沒有其他額外的資源申請,所有操作均為內存操作,無`IO`消耗。
<br>
## 方法
* `Channel->push`:當隊列中有其他協程正在等待`pop`數據時,自動按順序喚醒一個消費者協程。當隊列已滿時自動`yield`讓出控制器,等待其他協程消費數據
* `Channel->pop`:當隊列為空時自動`yield`,等待其他協程生產數據。消費數據后,隊列可寫入新的數據,自動按順序喚醒一個生產者協程。
> Coroutine\Channel使用本地內存,不同的進程之間內存是隔離的。只能在同一進程的不同協程內進行`push`和`pop`操作
> Coroutine\Channel在2.0.13或更高版本可用
<br>
## 屬性
* `$capacity`通道緩沖區容量
* `$errCode`channel錯誤碼
<br>
## 示例
~~~
use Swoole\Coroutine as co;
$chan = new co\Channel(1);
co::create(function () use ($chan) {
for($i = 0; $i < 100000; $i++) {
co::sleep(1.0);
$chan->push(['rand' => rand(1000, 9999), 'index' => $i]);
echo "$i\n";
}
});
co::create(function () use ($chan) {
while(1) {
$data = $chan->pop();
var_dump($data);
}
});
swoole_event::wait();
~~~
<br>
使用`Chan`可以方便得實現連接池功能。管理各類`Socket`連接資源。
## 連接池
~~~
class RedisPool
{
/**
* @var \Swoole\Coroutine\Channel
*/
protected $pool;
/**
* RedisPool constructor.
* @param int $size 連接池的尺寸
*/
function __construct($size = 100)
{
$this->pool = new Swoole\Coroutine\Channel($size);
for ($i = 0; $i < $size; $i++)
{
$redis = new Swoole\Coroutine\Redis();
$res = $redis->connect('127.0.0.1', 6379);
if ($res == false)
{
throw new RuntimeException("failed to connect redis server.");
}
else
{
$this->put($redis);
}
}
}
function put($redis)
{
$this->pool->push($redis);
}
function get()
{
return $this->pool->pop();
}
}
~~~
- 序章
- 1.環境搭建
- PHP7源碼編譯安裝
- Swoole源碼編譯安裝
- Mysql5.7源碼安裝
- Redis安裝
- 2.搭建Echo服務器
- 3.Server服務器
- 函數列表
- Server::__construct
- Server->set
- Server->on
- Server->start
- Server->send
- WebSocket
- Server->push
- Server->exist
- Server::pack
- Server::unpack
- Server->disconnect
- Server->isEstablished
- 配置選項
- reactor_num
- worker_num
- max_request
- max_conn
- daemonize
- backlog
- log_file
- log_level
- upload_tmp_dir
- http_parse_post
- document_root
- http_compression
- 事件回調函數
- onStart
- onWorkerStart
- onConnect
- onReceive
- onPacket
- onRequest
- 請求Request
- Http\Request->$header
- Http\Request->$server
- Http\Request->$get
- Http\Request->$post
- Http\Request->$cookie
- Http\Request->$files
- Http\Request->rawContent
- Http\Request->getData
- 響應Response
- Http\Response->header
- Http\Response->cookie
- Http\Response->status
- Http\Response->redirect
- Http\Response->write
- Http\Response->sendfile
- Http\Response->end
- Http\Response->detach
- Http\Response::create
- onClose
- onOpen
- onMessage
- 創建服務器
- TCP服務器
- UDP服務器
- HTTP服務器
- WebSocket服務器
- 4.定時器Timer
- 5.進程Process
- Process::__construct
- Process->start
- Process->name
- Process->exec
- Process->write
- Process->read
- Process->setTimeout
- Process->setBlocking
- Process->useQueue
- Process->statQueue
- Process->freeQueue
- Process->push
- Process->pop
- Process->close
- Process->exit
- Process::kill
- Process::wait
- Process::daemon
- Process::signal
- 6.內存Memory
- Table
- Table->__construct
- Table->column
- Table->create
- Table->set
- Table->incr
- Table->decr
- Table->get
- Table->exist
- Table->count
- Table->del
- Channel
- Channel->__construct
- Channel->push
- Channel->pop
- Channel->stats
- 7.協程Coroutine
- Coroutine
- Coroutine::list
- Coroutine::set
- Coroutine::stats
- Coroutine::create
- Coroutine::exist
- Coroutine::getCid
- Coroutine::getContext
- Coroutine::defer
- Coroutine::getBackTrace
- Coroutine::yield
- Coroutine::resume
- Coroutine::fread
- Coroutine::fgets
- Coroutine::fwrite
- Coroutine::sleep
- Coroutine::gethostbyname
- Coroutine::getaddrinfo
- Coroutine::exec
- Coroutine::readFile
- Coroutine::writeFile
- Coroutine::statvfs
- Coroutine::getPcid
- Coroutine\Channel
- Coroutine\Channel->__construct
- Coroutine\Channel->push
- Coroutine\Channel->pop
- Coroutine\Channel->stats
- Coroutine\Channel->close
- Coroutine\Channel->length
- Coroutine\Channel->isEmpty
- Coroutine\Channel->isFull
- Coroutine\Channel->$capacity
- Coroutine\Channel->$errCode
- Coroutine\Client
- Coroutine\Client->connect
- Coroutine\Client->send
- Coroutine\Client->recv
- Coroutine\Client->close
- Coroutine\Client->peek
- Coroutine\Client->set
- Coroutine\Http\Client
- Coroutine\Http\Client->get
- Coroutine\Http\Client->post
- 其他
- 并行和并發的區別
- 堆、棧、隊列