## 协程Coroutine
```
go(function () {
$redis = new Swoole\Coroutine\Redis();
$redis->connect('127.0.0.1', 6379);
$val = $redis->get('key');
});
```
<br>
协程可以理解为纯用户态的线程,其通过**协作**而不是抢占来进行切换。相对于进程或者线程,协程所有的操作都可以在用户态完成,创建和切换的消耗更低。`Swoole`可以为每一个请求创建对应的协程,根据`IO`的状态来合理的调度协程,这会带来了以下优势:
1. 开发者可以无感知的用同步的代码编写方式达到异步IO的效果和性能,避免了传统异步回调所带来的离散的代码逻辑和陷入多层回调中导致代码无法维护
2. 同时由于底层封装了协程,所以对比传统的`PHP`层协程框架,开发者不需要使用`yield`关键词来标识一个协程`IO`操作,所以不再需要对`yield`的语义进行深入理解以及对每一级的调用都修改为`yield`,这极大的提高了开发效率
<br>
## 协程应用场景:
类型ajax的异步操作。大数据处理,直接返回结果,耗时操作放协程里执行完成再返回结果。同步的编程逻辑实现异步的效果。
```
go(function () {
co::sleep(2);
echo "hello".PHP_EOL;
});
echo 'ok';
```
输出:
```
ok
hello
```
- 序章
- 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
- 其他
- 并行和并发的区别
- 堆、栈、队列