### 實戰:實現waitGroup功能
利用Swoole提供的Channel實現一個 waitGroup ,主要功能是用來等待所有協程執行完成的。
```php
<?php
class WaitGroup{
private $count;
private $chan;
public function __construct()
{
$this->chan = new Swoole\Coroutine\Channel();
}
public function add(){
$this->count++;
}
public function done(){
$this->chan->push(true);
}
public function wait(){
for($i=0;$i<$this->count;$i++){
$this->chan->pop();
}
}
}
<?php
include 'waitgroup.php';
Swoole\Runtime::enableCoroutine(true);
echo "start".PHP_EOL;
$t = microtime(true);
go(function() use ($t){
$wg = new WaitGroup();
$wg->add();
go(function() use ($t,&$wg){
echo file_get_contents("https://www.sunnyos.com/swoole.php");
echo "協程1:".(microtime(true)-$t).PHP_EOL;
$wg->done();
});
$wg->add();
go(function() use ($t,&$wg){
echo file_get_contents("https://www.sunnyos.com/swoole.php");
echo "協程2:".(microtime(true)-$t).PHP_EOL;
$wg->done();
});
$wg->add();
go(function() use ($t,&$wg){
echo file_get_contents("https://www.sunnyos.com/swoole.php");
echo "協程3:".(microtime(true)-$t).PHP_EOL;
$wg->done();
});
$wg->wait();
echo '全部結束:'.(microtime(true)-$t).PHP_EOL;
});
echo "end".PHP_EOL;
echo microtime(true)-$t.PHP_EOL;
```
- 第一章:基礎知識
- 課程簡介
- PHP-FPM過渡常駐內存
- 進程
- 實戰:實現Master-Worker
- 線程
- 實戰:CC攻擊器
- 協程
- 實戰:實現waitGroup功能
- 進程、線程、協程的區別
- 第二章:初識Swoft2.0
- Swoft介紹
- Swoft環境安裝
- gcc升級
- 安裝Swoft框架
- 目錄結構介紹
- SwoftCli工具
- Swoft配置
- 第三章:Swoft2.0核心
- 上下文
- 常駐內存沒有上下文隔離
- 實戰:手寫swoole框架上下文管理
- Bean容器
- 實戰:根據容器原理實現容器
- 實戰:通過容器實現依賴注入
- Bean容器定義與使用
- 配置文件定義Bean
- 容器類型
- 面向接口的容器
- 注解
- 實戰:實現注解
- 自定義Swoft注解類
- 事件
- 連接池
- 實戰:Swoole實現連接池
- 第四章:Http服務器
- Http Server生命周期
- Http Server配置
- 控制器
- 路由
- 請求對象Request
- 響應對象Response
- Http異常處理
- 中間件
- 實戰:中間件實現JWT登陸授權
- 第五章:驗證器
- 內置驗證類型
- 驗證器的使用
- 自定義驗證器
- 第六章:數據庫操作
- 連接數據庫
- 實體模型
- 模型事件
- 查詢器
- 事務處理
- 連接池配置
- 讀寫分離
- 多數據庫切換
- Models分層結構
- 實戰:實現用戶CURD API
- 第七章:Redis
- 連接redis和使用
- Redis連接池
- Redis集群配置(單機版)
- Redis集群配置(多服務器)
- Redis連接集群
- Redis實戰:實現延時任務
- 第八章:AOP編程
- AOP概念
- AOP實現原理
- 實戰實現AOP:靜態代理
- 實戰實現AOP:動態代理
- 切面注解介紹
- PointExecution切面
- PointBean切面
- PointAnnotation切面
- 實戰:使用AOP實現日志記錄
- 第九章:任務處理
- 進程使用
- 進程池使用
- 實戰:進程消費隊列
- 實戰:進程實現RabbitMQ延時隊列
- 異步任務
- 協程任務
- 定時任務