Swoole scheduler

The swoole server provides the methods swoole_server->tick and swoole_server->after to realize the asynchronous timer. For the swoole client, the swoole also provides the functions to realize the asynchronous timer. These schedule functions are accurate to milliseconds.

The swoole timer is based on timerfd and epoll.

public static void Swoole\Timer::tick ( integer $interval_ms , callable $callback [, string $param ] )

Alias: int swoole_timer_tick(int $duration_ms, callable $callback, mixed $param = NULL)

Trigger a timer tick by interval.

Parameter

Return

The timer id

Example

<?php
function test($timerid, $param)
{
    var_dump($timerid);
    var_dump($param);
}
swoole_timer_tick(1500, "test",["param1", "param2"]);

public static void Swoole\Timer::after ( int $after_time_ms , callable $callback )

Alias: swoole_timer_after(int $after_duration_ms, callable $callback, mixed $param = NULL)

Trigger a one time callback function in the future.

Parameter

Return

The timer id

Example

<?php
function test($param)
{
    var_dump(func_get_args());
    var_dump($param);
}

swoole_timer_after(1500, "test",["param1", "param2"]);

public static void Swoole\Timer::clear ( integer $timer_id )

Alias: boolean swoole_timer_clear($timer_id)

Cancel the timer tick or one time tick. Alias of function swoole_timer_clear.

Parameter

Return

bool

Example

<?php
$count = 0;
function test($timerid, $param)
{
    global $count;
    $count++;
    var_dump($count);
    if($count >= 10)
    {
        swoole_timer_clear($timerid);
    }
}

swoole_timer_tick(1000, "test",["param1", "param2"]);