# Timer定時器
---
## 定時器原理
Timer定時器是Swoole擴展提供的一個毫秒級定時器,其作用是每隔指定的時間間隔之后執行一次指定的回調函數,以實現定時執行任務的功能。
新版本的Swoole中,定時器是基于epoll方法的timeout機制實現的,不再依賴于單獨的定時線程,準確度更高。同時,Swoole擴展使用最小堆存儲定時器,減少定時器的檢索次數,提高了運行效率。
## 定時器使用
在Swoole中,定時器的函數原型如下:
```php
// function onTimer(int $timer_id, mixed $params = null); // 回調函數的原型
int swoole_timer_tick(int $ms, mixed $callback, mixed $param = null);
int swoole_server::tick(int $ms, mixed $callback, mixed $param = null);
// function onTimer(); // 回調函數的原型(不接受任何參數)
void swoole_timer_after(int $after_time_ms, mixed $callback_function);
void swoole_server::after(int $after_time_ms, mixed $callback_function);
```
tick定時器是一個永久定時器,使用tick方法創建的定時器會一直運行,每隔指定的毫秒數之后執行一次callback函數。在創建定時器的時候,可以通過tick函數的第三個參數,傳遞一些自定義參數到callback回調函數中。另外,也可以使用PHP的閉包(use關鍵字)實現傳參。具體實例如下:
```php
$str = "Say ";
$timer_id = swoole_timer_tick( 1000 , function($timer_id , $params) use ($str) {
echo $str . $params; // 輸出“Say Hello”
} , "Hello" );
```
tick函數會返回定時器的id。當我們不再需要某個定時器的時候,可以根據這個id,調用`swoole_timer_clear`函數刪除定時器。需要注意的是,創建的定時器是不能跨進程的,因此,在一個Worker進程中創建的定時器,也只能在這個Worker進程中刪除,這一點一定要注意(使用`$worker_id`變量來區分Worker進程);
after定時器是一個臨時定時器。使用after方法創建的定時器僅在指定毫秒數之后執行一次callback函數,執行完成后定時器就會刪除。after定時器的回調函數不接受任何參數,可以通過閉包方式傳遞參數,也可以通過類成員變量的方式傳遞。具體實例如下:
```php
class Test
{
private $str = "Say Hello";
public function onAfter()
{
echo $this->str; // 輸出”Say Hello“
}
}
$test = new Test();
swoole_timer_after(1000, array($test, "onAfter"); // 成員變量
swoole_timer_after(2000, function() use($test){ // 閉包
$test->onAfter(); // 輸出”Say Hello“
});
```
- 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