## 隊列(Queue)
異步并發的服務器里經常使用隊列實現生產者消費者模型,解決并發排隊問題。PHP的SPL標準庫中提供了SplQueue擴展內置的隊列數據結構。另外PHP的數組也提供了array_pop和array_shift可以使用數組模擬隊列數據結構。
**SplQueue**
~~~
$queue = new SplQueue;
//入隊
$queue->push($data);
//出隊
$data = $queue->shift();
//查詢隊列中的排隊數量
$n = count($queue);
~~~
**Array模擬隊列**
~~~
$queue = array();
//入隊
$queue[] = $data;
//出隊
$data = array_shift($queue);
//查詢隊列中的排隊數量
$n = count($queue);
~~~
**性能對比**
雖然使用Array可以實現隊列,但實際上性能會非常差。在一個大并發的服務器程序上,建議使用SplQueue作為隊列數據結構。
100萬條數據隨機入隊、出隊,使用SplQueue僅用2312.345ms即可完成,而使用Array模擬的隊列的程序根本無法完成測試,CPU一直持續在100%。
降低數據條數到1萬條后(100倍),也需要260ms才能完成測試。
SplQueue
~~~
$splq = new SplQueue;
for($i = 0; $i < 1000000; $i++)
{
$data = "hello $i\n";
$splq->push($data);
if ($i % 100 == 99 and count($splq) > 100)
{
$popN = rand(10, 99);
for ($j = 0; $j < $popN; $j++)
{
$splq->shift();
}
}
}
$popN = count($splq);
for ($j = 0; $j < $popN; $j++)
{
$splq->pop();
}
~~~
Array隊列
~~~
$arrq = array();
for($i = 0; $i <1000000; $i++)
{
$data = "hello $i\n";
$arrq[] = $data;
if ($i % 100 == 99 and count($arrq) > 100)
{
$popN = rand(10, 99);
for ($j = 0; $j < $popN; $j++)
{
array_shift($arrq);
}
}
}
$popN = count($arrq);
for ($j = 0; $j < $popN; $j++)
{
array_shift($arrq);
}
~~~
## 定長數組(SplFixedArray)
PHP官方的SPL庫提供了一個定長數組的數據結構,類似與C語言中的數組。和普通的PHP數組不同,定長數組讀寫性能更好。
**官方測試數據**
測試使用PHP 5.4,64位Linux系統
* small data (1,000):
* write: SplFixedArray is 15 % faster
* read: SplFixedArray is 5 % faster
* larger data (512,000):
* write: SplFixedArray is 33 % faster
* read: SplFixedArray is 10 % faster
**使用方法**
SplFixedArray使用方法與Array相同,但只支持數字索引的訪問方式。
~~~
$array = new SplFixedArray(5);
$array[1] = 2;
$array[4] = "foo";
var_dump($array[0]); // NULL
var_dump($array[1]); // int(2)
~~~
可以使用setSize()方法動態改變定長數組的尺寸。
- 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