php8.5
Home/ Manual/ swoole / event/ Swoole\Event::cycle

Swoole\Event::cycle

PHP function Edit on GitHub ✎

Define a function to execute at the end of each event loop iteration

Description

Swoole\Event::cycle(callable $callback, bool $before = false): bool

Defines a callback function to execute at the end (or beginning, if before is true) of each event loop iteration.

Parameters

callback

The function to execute. Set to null to clear a previously set cycle function.

before

If true, the callback executes before the event loop; if false, after.

Return Values

Success

Examples

Basic usage

php
<?php
Swoole\Timer::tick(2000, function ($id) {
    var_dump($id);
});

Swoole\Event::cycle(function () {
    echo "hello [1]\n";
    Swoole\Event::cycle(function () {
        echo "hello [2]\n";
        Swoole\Event::cycle(null);
    });
});

Swoole\Event::wait();

Source: reference/swoole/swoole/event/cycle.xml · from the official PHP manual (php/doc-en)