Mastering Swoole Timer: tick, after, clear and Advanced Methods Explained
This article provides a detailed guide to Swoole's millisecond‑precision timer API, covering the underlying epoll_wait/setitimer implementation, the static methods tick(), after(), clear() and additional utilities such as clearAll(), info(), list(), stats() and set(), with full syntax, usage examples and code snippets for both synchronous and asynchronous processes.
Overview
Swoole provides a high‑performance timer component with millisecond precision. It is built on epoll_wait (or kevent / poll / select for asynchronous I/O) and setitimer, using a min‑heap to manage a large number of timers efficiently. The timer API is available from Swoole v4.4.0 onward.
Static Methods
Swoole\Timer::tick()– alias
swoole_timer_tick() Swoole\Timer::after()– alias
swoole_timer_after() Swoole\Timer::clear()– alias
swoole_timer_clear()tick() – Repeating Timer
Description: Creates a periodic timer that fires every $msec milliseconds until it is explicitly cleared with Swoole\Timer::clear().
Signature:
int Swoole\Timer::tick(int $msec, callable $callback, mixed ...$params);Example:
$timer_id = Swoole\Timer::tick(3000, function (int $timer_id, $param1, $param2) {
echo "timer_id #{$timer_id}, after 3000ms.
";
echo "param1 is {$param1}, param2 is {$param2}.
";
// schedule another tick after 14000ms
Swoole\Timer::tick(14000, function (int $timer_id) {
echo "timer_id #{$timer_id}, after 14000ms.
";
});
}, "A", "B");after() – One‑Shot Timer
Description: Executes the callback once after the specified delay and then destroys the timer.
Signature:
int Swoole\Timer::after(int $msec, callable $callback, mixed ...$params);Example:
$str = "Swoole";
Swoole\Timer::after(1000, function () use ($str) {
echo "Hello, {$str}
";
});clear() – Remove a Timer
Description: Clears a timer identified by its ID.
Signature: bool Swoole\Timer::clear(int $timer_id); Example:
$timer = Swoole\Timer::after(1000, function () {
echo "timeout
";
});
var_dump(Swoole\Timer::clear($timer)); // bool(true)Additional Utility Methods
Swoole\Timer::clearAll()– clears all timers in the current worker process. Swoole\Timer::info(int $id) – returns an associative array with information about the specified timer. Swoole\Timer::list() – returns an iterator that can be used to traverse all timer IDs. Swoole\Timer::stats() – provides overall timer statistics as an array. Swoole\Timer::set(array $settings) – configures timer parameters; for example, disabling coroutine support with ['enable_coroutine' => false].
Usage Context
In synchronous I/O processes (e.g., Manager and TaskWorker), timers are implemented with setitimer and signals. In asynchronous I/O processes, the timeout mechanisms of epoll_wait, kevent, poll or select are used.
The code snippets above demonstrate how to create periodic and one‑shot timers, pass additional parameters, and cancel timers, providing a practical reference for integrating high‑precision timers into Swoole‑based PHP applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
