# 4.0.1
## 主要更新
* 支持MySQL8全新的caching_sha2_password密碼驗證算法
* 增加enable_coroutine配置項,用于關閉自動創建協程
* 移除--enable-coroutine編譯配置
* 修復chan->push無法立即喚醒等待協程的問題
## 關閉內置協程
4.0.1之前的版本,底層在Server和Timer的事件回調函數中會自動創建協程,如果回調函數中沒有使用任何協程API,這會浪費一次協程創建/銷毀操作。而且無法與1.x保持兼容。
新版本中增加了enable_coroutine配置項,可關閉內置協程。用戶可根據需要,在回調函數中自行使用Coroutine::create或go創建協程。關閉內置協程后,底層與1.x版本的行為保持了一致性,實現了100%兼容。原先使用1.x的框架,也可以直接基于4.0作為運行環境。
現在可以動態選擇是否啟用內置協程,那么--enable-coroutine編譯配置變得可有可無了。新版本中移除了該編譯選項。
```php
use Swoole\Http\Request;
use Swoole\Http\Response;
$http = new swoole_http_server('127.0.0.1', 9501);
$http->set([
'enable_coroutine' => false, // close build-in coroutine
]);
$http->on('workerStart', function () {
echo "Coroutine is " . (Co::getuid() > 0 ? 'enable' : 'disable')."\n";
});
$http->on("request", function (Request $request, Response $response) {
$response->header("Content-Type", "text/plain");
if ($request->server['request_uri'] == '/co') {
//關閉內置協程后,需要手工創建協程
go(function () use ($response) {
$response->end("Hello Coroutine #" . Co::getuid());
});
} else {
//沒有任何協程操作,這里不存在額外的協程調度開銷
$response->end("Hello Swoole #" . Co::getuid());
}
});
$http->start();
```
# 4.0.0
## 全新協程內核
新版本4.0基于boost.context 1.60匯編代碼實現了全新的協程內核。在保存PHP函數調用棧的基礎上,增加了C棧的上下文存儲。實現了對所有PHP語法的支持。現在在任意PHP的函數,包括call_user_func、反射、魔術方法、array_map中均可使用協程。
**4.0與2.0是100%兼容的,僅重構了協程內核,API層無變更**
> 4.0分支代碼升級至C++11標準,建議使用gcc-4.8或更高版本
> 僅支持php7.1及以上版本
## 全局變量隔離
新版本中底層對全局變量進行了隔離,現在可以使用Swoole\Process創建多個Swoole\Server實例了。
```php
for ($i = 0; $i < 2; $i++)
{
$p = new swoole_process(function () use ($i) {
$port = 9501 + $i;
$http = new swoole_http_server("127.0.0.1", $port);
$http->on("start", function ($server) use ($port) {
echo "Swoole http server is started at http://127.0.0.1:{$port}\n";
});
$http->on("request", function ($request, $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello World\n");
});
$http->start();
}, false, false);
$p->start();
}
```
### 其他更新
* 修復http2服務器無法向Chrome瀏覽器客戶端發送超過16K數據的問題
* 增加Channel->peek方法,用于窺視數據
* 修復Server->pause/resume在SWOOLE_PROCESS下無法使用的問題
* 移除Linux AIO,現在無論如何設置都使用線程池實現異步文件IO
* 支持MySQL存儲過程
- swoole簡介
- swoole功能概述
- 序章
- 開發必讀
- 1 環境搭建
- 1.1 環境搭建
- 1.2 搭建Echo服務器
- 2 初識Swoole
- 2.1 Worker進程
- 2.2 TaskWorker進程
- 2.3 Timer定時器
- 2.4 Process進程
- 2.5 Table內存表
- 2.6 多端口監聽
- 2.7 sendfile文件支持
- 2.8 SSL支持
- 2.9 熱重啟
- 2.10 http_server
- 附錄*server配置
- 附錄*server函數
- 附錄*server屬性
- 附錄*server回調函數
- 附錄*server高級特性
- 心跳檢測
- 3 Swoole協議
- 3.1 EOF協議
- 3.2 固定包頭協議
- 3.3 Http協議
- 3.4 WebSocket協議
- 3.5 MTQQ協議
- 內置http_server
- 內置websocket_server
- Swoole\Redis\Server
- 4 Swoole異步IO
- 4.1 AsyncIO
- 異步文件系統IO
- swoole_async_readfile
- swoole_async_writefile
- swoole_async_read
- swoole_async_write
- 5 swoole異步客戶端
- ws_client
- http_client
- mysql_client
- redis_client
- tcp_client
- http2_client
- 6 swoole協程
- Swoole\Coroutine\Http\Client
- Swoole\Coroutine\MySQL
- Swoole\Coroutine\Redis
- Coroutine\PostgreSQL
- Swoole\Coroutine\Client
- Swoole\Coroutine\Socket
- Swoole\Coroutine\Channel
- Coroutine
- Swoole\Coroutine::create
- Swoole\Coroutine::resume
- Swoole\Coroutine::suspend
- Swoole\Coroutine::sleep
- Coroutine::getaddrinfo
- Coroutine::gethostbyname
- swoole_async_dns_lookup_coro
- Swoole\Coroutine::getuid
- getDefer
- setDefer
- recv
- Coroutine::stats
- Coroutine::fread
- Coroutine::fget
- Coroutine::fwrite
- Coroutine::readFIle
- Coroutine::writeFIle
- Coroutine::exec
- 7 swoole_process
- process::construct
- process::start
- process::name
- process::signal
- process::setaffinity
- process::exit
- process::kill
- process::daemon
- process->exec
- process::wait
- process::alarm
- 8 swoole定時器
- swoole_timer_tick
- swoole_timer_after
- swoole_timer_clear
- 9 swoole_event
- swoole_event_add
- swoole_event_set
- swoole_event_del
- swoole_event_wait
- swoole_event_defer
- swoole_event_write
- swoole_event_exit
- swoole提供的function
- 常見問題
- 客戶端鏈接失敗原因
- 如何設置進程數
- 如何實現異步任務
- 如何選擇swoole三種模式
- php中哪些函數是阻塞的
- 是否可以共用1個redis或mysql連接
- 如何在回調函數中訪問外部的變量
- 為什么不要send完后立即close
- 不同的Server程序實例間如何通信
- MySQL的連接池、異步、斷線重連
- 在php-fpm或apache中使用swoole
- 學習Swoole需要掌握哪些基礎知識
- 在phpinfo中有在php-m中沒有
- 同步阻塞與異步非阻塞選擇
- CURL發送POST請求服務器端超時
- 附錄
- 預定義常量
- 內核參數調優
- php四種回調寫法
- 守護進程程序常用數據結構
- swoole生命周期
- swoole_server中內存管理機制
- 使用jemalloc優化swoole內存分配性能
- Reactor、Worker、Task的關系
- Manager進程
- Swoole的實現
- Reactor線程
- 安裝擴展
- swoole-worker手冊
- swoole相關開源項目
- 寫在后面的話
- 版本更新記錄
- 4.0