# swoole_async_IO
---
[TOC=2,3]
---
## **1.swoole_async_readfile**
功能:異步讀取文件內容<br>
函數原型:<br>
```php
swoole_async_readfile(string $filename, mixed $callback);
```
參數說明:<br>
| 參數 | 描述 |
| -------- | ----- |
| filename | 待讀取的文件名 |
| callback | 讀取結束的回調函數 |
回調函數原型:<br>
```php
function callback($filename, $content);
```
回調函數參數說明:<br>
| 參數 | 描述 |
| -------- | ----- |
| filename | 文件名 |
| content | 讀取到的文件內容 |
說明:<br>
swoole_async_readfile會將文件內容全部復制到內存,所以不能用于大文件的讀取<br>
如果要讀取超大文件,請使用[swoole_async_read](#3swoole_async_read)函數<br>
swoole_async_readfile最大可讀取**4M**的文件,受限于**SW_AIO_MAX_FILESIZE**宏<br>
示例:<br>
```php
swoole_async_readfile( __DIR__."/Test.txt", function($filename, $content) {
echo "$filename: $content";
});
```
## **2.swoole_async_writefile**
功能:異步寫文件<br>
函數原型:<br>
```php
swoole_async_writefile(string $filename, string $content, mixed $callback);
```
參數說明:<br>
| 參數 | 描述 |
| -------- | ----- |
| filename | 待寫入的文件名 |
| content | 待寫入的文件內容 |
| callback | 寫入結束的回調函數 |
回調函數原型:<br>
```php
function callback($filename);
```
回調函數參數說明:<br>
| 參數 | 描述 |
| -------- | ----- |
| filename | 文件名 |
說明:<br>
swoole_async_writefile最大可寫入4M的文件,也可以不指定回調函數<br>
示例:<br>
```php
swoole_async_writefile('test.log', "This is a test log", function($filename) {
echo "wirte ok.\n";
});
```
## **3.swoole_async_read**
功能:異步分段讀文件<br>
函數原型:<br>
```php
swoole_async_read(string $filename, mixed $callback, int $trunk_size = 8192);
```
參數說明:<br>
| 參數 | 描述 |
| -------- | ----- |
| filename | 待讀取的文件名 |
| callback | 讀取結束的回調函數 |
| trunk_size | 每次讀取的字節數 |
回調函數原型:<br>
```php
function callback($filename, $content);
```
回調函數參數說明:<br>
| 參數 | 描述 |
| -------- | ----- |
| filename | 文件名 |
| content | 讀取到的文件內容,如果為空代表文件已讀完。 |
說明:<br>
swoole_async_read每次讀取trunk_size個字節,讀完后會自動調用callback函數<br>
在callback回調函數中,可以通過`return true/false`來控制是否繼續讀取文件。返回true代表繼續讀取,返回false代表停止讀取。
示例:<br>
```php
swoole_async_read( __DIR__."/Test.txt" , function($filename, $content){
if( empty( $content ) ) {
return false;
} else {
echo "$filename: $content";
return true;
}
} , 8192 );
```
## **4.swoole_async_write**
功能:異步寫文件<br>
函數原型:<br>
```php
bool swoole_async_write(string $filename, string $content, int $offset = -1, mixed $callback = NULL);
```
參數說明:<br>
| 參數 | 描述 |
| -------- | ----- |
| filename | 待寫入的文件名 |
| content | 待寫入的文件內容 |
| offset | 寫入文件的位置 |
| callback | 寫入結束的回調函數 |
回調函數原型:<br>
```php
function callback($filename, $writen);
```
回調函數參數說明:<br>
| 參數 | 描述 |
| -------- | ----- |
| filename | 文件名 |
| writen | 本次調用已經寫入的字節數 |
說明:<br>
swoole_async_write過傳入的offset參數來確定寫入的位置,當offset為-1時表示追加寫入到文件的末尾<br>
示例:<br>
```php
swoole_async_write( 'test_1.log', "This is a test log\n" , -1 , function( $filename, $writen ){
echo "$filename: write $writen byte\n";
});
```
## **5.swoole_async_dns_lookup**
功能:將域名解析為IP地址<br>
函數原型:<br>
```php
bool swoole_async_dns_lookup(string $host, mixed $callback);
```
參數說明:<br>
| 參數 | 描述 |
| -------- | ----- |
| filename | 待查詢的地址 |
| callback | 查詢結束的回調函數 |
回調函數原型:<br>
```php
function callback($host, $ip);
```
回調函數參數說明:<br>
| 參數 | 描述 |
| -------- | ----- |
| filename | 文件名 |
| writen | 本次調用已經寫入的字節數 |
說明:<br>
當DNS查詢失敗時,比如域名不存在,回調函數傳入的$ip為空<br>
示例:<br>
```php
swoole_async_dns_lookup("www.baidu.com", function($host, $ip){
echo "{$host} : {$ip}\n";
});
```
- 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