## 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
- 其他
- 并行和并发的区别
- 堆、栈、队列