Swoole 中毫秒定时器(Timer)的使用
间隔定时器, tick
定时器会持续触发,直到调用 clear() 清除为止。
$timer = SwooleTimer::tick(3000, function (int $timer_id, $param1, $param2) {
echo "timer_id #$timer_id, after 3000ms.
";
echo "param1 is $param1, param2 is $param2.
";
SwooleTimer::tick(14000, function ($timer_id) {
echo "timer_id #$timer_id, after 14000ms.
";
});
}, "A", "B");
一次性定时器,执行完成后会自动销毁。
$str = "Swoole";
$timer = SwooleTimer::after(1000, function() use ($str) {
echo "Hello, $str
";
});
使用定时器 ID
来删除定时器。
var_dump(SwooleTimer::clear($timer));