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:30CRMEB

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++;

});

 Insert picture description here

  • 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";

});

 Insert picture description here

  • 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);

    }

});

 Insert picture description here

  • 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

Random recommended