In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to analyze the use of timers in Swoole, the content is very detailed, interested friends can refer to, hope to be helpful to you.
1. Introduction of the timer of PHP itself
Let's first talk about how native PHP implements timers. PHP timers are implemented through pcntl_alarm:
Pcntl_alarm (int $seconds): int
The pcntl_alarm function sets an alarm alarm signal for the process. When this method is called, a counter is created 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, and if $seconds is set to 0, the alarm signal will not be created. Returns the number of seconds left in the last alarm schedule after calling this function, or 0 if there is no previous alarm schedule.
Let's look at an example where the pcntl_signal () function installs the signal processor and pcntl_signal_dispatch () calls the processor waiting for the signal.
The pcntl_alarm () function is implemented based on the clock signal + tick function, which has some defects:
Maximum advance is supported to seconds, while Swoole Timer can reach millisecond level
Setting multiple timer programs at the same time is not supported
Pcntl_alarm () depends on declare (ticks = 1), so its performance is very poor.
2. The use of timer in Swoole
Timers in Swoole can achieve millisecond precision and support the addition of 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, callable $callback, … $params)
The alias for this function is swoole_timer_tick (). $msec is the specified time in milliseconds. $callback is the function executed after the time expires. The $callback callback function takes multiple arguments, the first of which is the ID of the timer. Note: the timer is valid only in the current process space. Timers are implemented asynchronously and cannot be used with functions that block IO, otherwise the execution time of timers will be out of order.
Here is a demo of the timer in the onMessage callback function of websocket
After triggering onMessage callback, the following information is printed on the client side, and screenshots are taken.
In addition, swoole also provides another function Timer::after to implement the timer, which is used to execute the callback function after the specified time. The function prototype is as follows:
Int Swoole\ Timer::after (int $after_time_ms, callable $callback_function, … $params)
The alias for this function is swoole_timer_after. Timer::after is an one-time timer that is destroyed after execution. The sample code is as follows:
In addition to the functions for setting timers, Swoole provides several other functions:
Function Timer::set (array $array); / / set parameters of the timer
Timer\ Iterator Timer::list (); / / returns the timer iterator, which can use foreach to traverse the id of all global timer
Array Timer::info (int $id); / / returns the information of timer
Bool Swoole\ Timer::clear (int $timer_id); / / use timer ID to delete timer
Bool Timer::clearAll (); / / clear all timers in the current worker process
Third, the underlying principle of Swoole timer.
The underlying Swoole timer is implemented based on epoll_wait and setitimer, and the data structure uses the smallest heap.
The execution time of the Swoole timer callback function does not affect the time of the next timer execution. If the execution time of the timer callback function is too long, it even covers the time of the next timer execution. The underlying layer will do time correction, discard expired behavior, and call back at the next time. On how to analyze the use of timers in Swoole to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.