diff --git a/reference/swoole/swoole.timer.xml b/reference/swoole/swoole.timer.xml index 035a6093c494..865dc153aba9 100644 --- a/reference/swoole/swoole.timer.xml +++ b/reference/swoole/swoole.timer.xml @@ -12,8 +12,58 @@
&reftitle.intro; + Millisecond precision timer. The underlying implementation is based on epoll_wait and setitimer, + using a min-heap data structure that supports adding a large number of timers. + + + In synchronous IO processes, it's implemented using setitimer and signals, such as in Manager and TaskWorker processes. + + + In asynchronous IO processes, it's implemented using the timeout of epoll_wait/kevent/poll/select. + + + The underlying system does not support timers with a time parameter of 0. This is different from languages like Node.js. + In Swoole, you can use Swoole\Event::defer to achieve similar functionality. + + +]]> + + + + Timer Correction: The execution time of the timer callback function does not affect the timing of the + next timer execution. For example, setting a tick timer of 10ms after 0.002s, the first callback will + be executed at 0.012s, if the callback function takes 5ms to execute, the next timer will still trigger + at 0.022s, not at 0.027s. + However, if the execution time of the timer callback function is too long, even covering the time of the + next timer execution, the underlying system will perform time correction, discarding the expired behavior + and triggering the timer callback at the next available time. For example, if the callback function + at 0.012s takes 15ms to execute, causing the timer at 0.022s to be delayed, the timer callback + will be triggered again at 0.032s. + + + By default, when a timer is triggered, a coroutine is automatically created to execute the callback function. + + + Timer only works within the current process space. + + + + + Timer is purely asynchronous and incompatible with synchronous IO functions. + + + + + Timer execution may experience minor timing deviations. + +
diff --git a/reference/swoole/swoole/timer/after.xml b/reference/swoole/swoole/timer/after.xml index 6aa828e536f0..21f97b096632 100644 --- a/reference/swoole/swoole/timer/after.xml +++ b/reference/swoole/swoole/timer/after.xml @@ -4,38 +4,47 @@ Swoole\Timer::after - Trigger a callback function after a period of time. + Execute a function after specified time. &reftitle.description; - public static voidSwoole\Timer::after - intafter_time_ms - callablecallback + public static intSwoole\Timer::after + intmsec + callablecallback_function + mixedparams - Trigger a callback function after a period of time. + Execute a function after a specified time. The Swoole\Timer::after function is a one-time timer that will be + destroyed once executed. - &reftitle.parameters; - after_time_ms + msec - + Specified time in milliseconds (e.g. 1000 means 1 second). - callback + callback_function - + The callback function to be executed when timer expires. + + + + + params + + + Additional parameters to pass to the callback function. @@ -45,11 +54,28 @@ &reftitle.returnvalues; - + Returns timer ID which can be used to clear the timer. - + + &reftitle.examples; + + + <function>Swoole\Timer::after</function> example + + +]]> + + + + + + + + Swoole\Timer::clearAll + Clear all timers in current worker process. + + + + &reftitle.description; + + public static boolSwoole\Timer::clearAll + + + + Clear all timers in current worker process. Available since Swoole version >= v4.4.0. + + + + + &reftitle.returnvalues; + + &return.success; + + + + + diff --git a/reference/swoole/swoole/timer/exists.xml b/reference/swoole/swoole/timer/info.xml similarity index 65% rename from reference/swoole/swoole/timer/exists.xml rename to reference/swoole/swoole/timer/info.xml index 50fd227fbf63..1e8d72c2c766 100644 --- a/reference/swoole/swoole/timer/exists.xml +++ b/reference/swoole/swoole/timer/info.xml @@ -1,22 +1,21 @@ - + - Swoole\Timer::exists - Check if a timer is existed. + Swoole\Timer::info + Get information about a timer. &reftitle.description; - public static boolSwoole\Timer::exists + public static arraySwoole\Timer::info inttimer_id - Check if a timer is existed. + Get information about a timer. Available since Swoole version >= v4.4.0. - @@ -26,7 +25,7 @@ timer_id - + The timer ID @@ -36,11 +35,18 @@ &reftitle.returnvalues; - + Returns an array with timer information: + int(6000) + ["exec_count"]=> int(5) + ["interval"]=> int(1000) + ["round"]=> int(0) + ["removed"]=> bool(false) + } + ]]> - - + + + + Swoole\Timer::list + Get iterator for all timers in current worker process. + + + + &reftitle.description; + + public static Swoole\Timer\IteratorSwoole\Timer::list + + + + Returns timer iterator which can be used to traverse all timer IDs in current worker process. + + + + + &reftitle.returnvalues; + + Returns Swoole\Timer\Iterator object. + + + + + &reftitle.examples; + + + <function>Swoole\Timer::list</function> example + + +]]> + + + + + + + diff --git a/reference/swoole/swoole/timer/set.xml b/reference/swoole/swoole/timer/set.xml new file mode 100644 index 000000000000..68fa0a5f8949 --- /dev/null +++ b/reference/swoole/swoole/timer/set.xml @@ -0,0 +1,61 @@ + + + + + + Swoole\Timer::set + Set timer parameters. + + + + &reftitle.description; + + public static voidSwoole\Timer::set + arrayarray + + + Set timer related parameters. + + + + + This method is deprecated since v4.6.0. + + + + + + &reftitle.parameters; + + + array + + + Array of timer parameters + + + + + + + + diff --git a/reference/swoole/swoole/timer/stats.xml b/reference/swoole/swoole/timer/stats.xml new file mode 100644 index 000000000000..059bc6d5e9e4 --- /dev/null +++ b/reference/swoole/swoole/timer/stats.xml @@ -0,0 +1,55 @@ + + + + + + Swoole\Timer::stats + Get timer statistics. + + + + &reftitle.description; + + public static arraySwoole\Timer::stats + + + + Get timer statistics. + + + + + &reftitle.returnvalues; + + Returns an array with timer statistics: + bool(true) + ["num"]=> int(1000) + ["round"]=> int(1) + } + ]]> + + + + + diff --git a/reference/swoole/swoole/timer/tick.xml b/reference/swoole/swoole/timer/tick.xml index 12da8aa17a7b..4ac82e76e4c2 100644 --- a/reference/swoole/swoole/timer/tick.xml +++ b/reference/swoole/swoole/timer/tick.xml @@ -4,47 +4,50 @@ Swoole\Timer::tick - Repeats a given function at every given time-interval. + + Set an interval timer, unlike the after timer, + the tick timer will trigger repeatedly until cleared by Timer::clear. + &reftitle.description; - public static voidSwoole\Timer::tick - intinterval_ms - callablecallback - stringparam + public static intSwoole\Timer::tick + intmsec + callablecallback_function + mixedparams - + Set an interval timer that will persist until cleared. Different from after timer, + tick timer will keep triggering until cleared by Timer::clear. - &reftitle.parameters; - interval_ms + msec - + Specified time in milliseconds (e.g. 1000 means 1 second) - callback + callback_function - + The callback function to be executed when timer expires - param + params - + Additional parameters to pass to the callback function @@ -54,11 +57,27 @@ &reftitle.returnvalues; - + Returns timer ID which can be used to clear the timer. - + + &reftitle.examples; + + + <function>Swoole\Timer::tick</function> example + + +]]> + + + +