current position:Home>Can you believe that the swoole timer can realize millisecond task scheduling?
Can you believe that the swoole timer can realize millisecond task scheduling?
2022-05-15 07:44:30【CRMEB】
brief introduction
Timer
Millisecond precision timer , Bottom based epoll_wait
and setitimer
Realization , Data structure use The smallest pile
, It can support adding a large number of timers , Timer implemented using minimum heap data structure , similar JavaScript Of setInterval
,Swoole
Addition and deletion of timer , All memory operations , So the performance is very high .
Swoole Medium Timer
And PHP
Of itself pcntl_alarm
Is different .pcntl_alarm
Is based on Clock signal + tick
Function implementation , One is that it only supports up to seconds , The other is that it does not support setting multiple timer programs at the same time , The performance is relatively poor .
Interval clock timer
We can go through Timer::tick
To realize the interval clock timer , The timer will continue to trigger , Automatically trigger the execution of callback function every specified time , Until the call Timer::clear
To clear the specified timer .
$i = 0;
Swoole\Timer::tick(1000, function($id) use ($i) {
global $i;
echo "tick id:$id i:$i \n";
$i++;
});
- every other 1 The callback function is triggered once in seconds , The callback function will automatically print a line of information to the console .
One time timer
When you need to execute a timer, you can use Timer::after
, This function is a one-time timer , Unlike the interval clock timer , It will be destroyed after the execution , It should be noted that Timer::after
It's non blocking .
Swoole\Timer::after(2000, function () {
echo " Do it once after\n";
});
- 2 Execute the callback function in seconds , Automatically exit after execution .
Clear timer
Timer::after
It will exit automatically after execution , There's no need to clean up , and Timer::tick
No clear timer operation , Will always carry out , Until the program exits . When you don't need a timer , We can use Timer::clear
To stop the timer , Set the corresponding timer id
Pass in this method .
Be careful ,Timer::clear
Timers that cannot be used to clear other processes , Only for the current process .
$i = 0;
Swoole\Timer::tick(1000, function ($id) use ($i) {
global $i;
echo "tick id:$id i:$i \n";
$i++;
if ($i > 10) {
Swoole\Timer::clear($id);
}
});
- every other 1 Execute the callback function once per second , When
$i
From to 10 when , off timer , The program will exit automatically .
Clear all timers
Timer::clearAll
Used to clear timers in the same process space . Basically with Timer::clear
identical , but Timer::clearAll
You can clear all active timers at once .
Swoole\Timer::tick(2000, function () {
echo "tick \n";
});
$str = "Swoole";
Swoole\Timer::after(1000, function() use ($str) {
echo "Hello, $str\n";
});
Swoole\Timer::clearAll();
ending
Timer function can be very convenient to achieve timing tasks , for example CRMEB Pro System
Auto cancel order in 、 Automatic confirmation of receipt 、 Automatic product loading and unloading 、 Automatic expiration of tenants and other operations are based on Timer::tick
To achieve .
CRMEB Pro System With excellent system performance , Personalized background operation , Rich marketing functions , Refined user operation , Fission distribution , Personalized communication , Solve the problem of e-commerce 、 Promotion problems , Help enterprises build an ecological class B2C Profit model Mall .
The source code attachment has been packaged and uploaded to Baidu cloud , You can download it yourself ~
link : https://pan.baidu.com/s/14G-bpVthImHD4eosZUNSFA?pwd=yu27
Extraction code : yu27
Baidu cloud link is unstable , It may fail at any time , Let's keep it tight .
If Baidu cloud link fails , Please leave me a message , When I see it, I will update it in time ~
Open source address
Code cloud address :
http://github.crmeb.net/u/defu
Github Address :
http://github.crmeb.net/u/defu
Open source is not easy ,Star Show respect , Interested friends welcome Star, Submit PR, Maintain open source projects together , Benefit more people !
copyright notice
author[CRMEB],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/131/202205102122310648.html
The sidebar is recommended
- C - no AC this summer
- Thread control - thread waiting, thread termination, thread separation
- Key points of acupuncture and moxibustion
- Module product and problem solution of Luogu p2260 [Tsinghua training 2012]
- Review points of Geodesy
- Summary of review points of Geodesy
- Arrangement of geodetic knowledge points
- Review key points of basic geodesy
- Luogu p2522 [haoi2011] problem B solution
- [app test] summary of test points
guess what you like
Version management tool - SVN
JDBC ~ resultset, use of resultsetmetadata, ORM idea, arbitrary field query of any table (JDBC Implementation)
This article takes you to understand can bus
Gear monthly update April
Gear monthly update April
Convert timestamp to formatted date JS
The time stamp shows how many minutes ago and how many days ago the JS was processed
[untitled]
Luogu p2216 [haoi2007] ideal square problem solution
Miscellaneous questions [2]
Random recommended
- Which securities company does qiniu school recommend? Is it safe to open an account
- Hyperstyle: complete face inversion using hypernetwork
- What activities are supported by the metauniverse to access reality at this stage?
- P2P swap OTC trading on qredo
- Google | coca: the contrast caption generator is the basic image text model
- SIGIR 2022 | Huawei reloop: self correcting training recommendation system
- Whether you want "melon seed face" or "national character face", the "face changing" technology of Zhejiang University video can be done with one click!
- Sorting of naacl2022 prompt related papers
- Servlet create project
- "Chinese version" Musk was overturned by the original: "if it's true, I want to see him"
- [network security] web security trends and core defense mechanisms
- [intensive reading] object detection series (10) FPN: introducing multi-scale with feature pyramid
- 007. ISCSI server chap bidirectional authentication configuration
- 2021-03-09
- plot_ Importance multi classification, sorting mismatch, image value not displayed
- [intensive reading] object detection series (XI) retinanet: the pinnacle of one stage detector
- How to install MFS environment for ECS
- [intensive reading] the beginning of object detection series (XII) cornernet: anchor free
- Open source sharing -- a record of students passing through time
- MOT:A Higher Order Metric for Evaluating Multi-object Tracking
- How to develop a distributed memory database (1)
- Reverse engineers reverse restore app and code, and localization is like this
- One line command teaches you how to export all the libraries in anaconda
- Bi tools are relatively big. Let's see which one is most suitable for you
- Read the history of database development
- Self cultivation of coder - batterymanager design
- Technology application of swift phantom type phantom in Apple source code learning
- Swiftui advanced skills: what is the use of the technology of swift phantom type phantom
- Swiftui advanced animation Encyclopedia of complex deformation animation is based on accelerate and vector arithmetic (tutorial includes source code)
- What problems remain unsolved in swiftui in 2022
- I'll set the route for fluent
- Flutter drawing process analysis and code practice
- Emoji language commonly used icon collection (interesting Emoji)
- 5.14 comprehensive case 2.0 - automatic induction door
- How to deploy redis service on k8s top?
- Importance of data warehouse specification
- Idea automatically generates serialization ID
- Why is it recommended not to use select * in MySQL?
- Let's talk about why redis needs to store two data structures for the same data type?
- Domain lateral move RDP delivery