This section will address the following three issues:
PHP itself timer introduction
The use of a timer in Swoole
Swoole timer underlying principle
PHP timer introduction
PHP timers are implemented via pCNTL_alarm:
Pcntl_alarm (int $seconds) : int
The pCNTL_alarm function sets an alarm signal for a process. Calling this method creates a counter that sends a SIGALRM signal to the process after the specified number of seconds. Each call to pCNTL_alarm cancels the previously set alarm signal.
Where, seconds is the number of seconds to wait, if seconds is the number of seconds to wait, if seconds is the number of seconds to wait, and if seconds is set to 0, no alarm signal will be created. This function returns the number of seconds remaining from the last alarm schedule, or 0 if there was no previous alarm schedule.
For an example, the pCNTL_signal () function installs the signal handler, and pCNTL_signal_dispatch () calls the handler waiting for the signal.
The pCNTL_alarm () function is based on the clock signal + tick function and has some defects:
Maximum input can be up to seconds, while Swoole Timer can be up to milliseconds
Multiple timer programs cannot be set at the same time
Pcntl_alarm () relies on DECLARE (ticks = 1) and has poor performance
Second, the method of using timer in Swoole
Swoole’s timers are millisecond accurate and can be added to a large number of timers.
In Swoole you can use Timer::tick to set an interval clock Timer and call Timer:: Clear to clear the Timer. The function is defined as follows:
Int Swoole\Timer::tick(int msec, callableMsec,callablemsec, CallAblecallBack,… $params);
The alias for this function is swoole_timer_tick(). Msec is the specified time in milliseconds. Msec is the specified time in milliseconds. Msec is the specified time in milliseconds. Callback is a function to be executed after the time expires. The $callback callback takes multiple arguments, the first of which is the timer ID. Note: Timers are only valid in the current process space. Timers are implemented purely asynchronously and cannot be used with functions that block I/O, otherwise the timer execution time will be distorted.
The following is a demo of the timer in webSocket’s onMessage callback,
After the onMessage callback is triggered, the following information is displayed on the client
Swoole also provides a Timer::after function that executes a callback after a specified time. The function prototype is as follows:
Int Swoole \ Timer: : after (int aftertimems, callableafter_time_ms, callable aftertimems, callablecallback_function,… $params);
The alias for this function is swoole_timer_after. Timer:: After is a one-time Timer that is destroyed after being executed. Example code is as follows:
In addition to the timer setting function, Swoole provides several other functions:
function Timer::set(array $array); // Set timer parameters
Timer\Iterator Timer::list(); // Return timer iterator, which can be used to iterate through all timer ids globally with foreach
array Timer::info(int $id); // Return timer information
bool Swoole\Timer::clear(int $timer_id); // Delete the timer with the timer ID
bool Timer::clearAll(); // Clear all timers in the current working process
Third, Swoole timer basic principle
Swoole timer is implemented based on epoll_WAIT and Setitimer, and the data structure uses the minimum heap.
The Swoole timer callback execution time does not affect the time of the next timer execution. If the timer callback takes too long, it may even overwrite the next timer execution. The bottom layer does time correction, discarding expired behavior and calling back at the next time.