OpenSwoole\Event::cycle

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Event::cycle(callable $callback, bool $before = false)\:\ bool

Parameters

callback

callback function to executed every event loop cycle

before

before or after the event loop

Return

success

If success, it returns TRUE, meaning the event listener was successfully added

Otherwise it returns FALSE, meaning there was an issue registering the event

Description

Register a callback function which is executed every loop, you can set if this function or callable executes before or after the event loop round:

bool $before:

  • false: Execute the function after the event loop round
  • true: Execute the function before the start of the event loop round

You can have two cycle functions operating when setting the before and after parameters

Example

<?php

OpenSwoole\Timer::tick(2000, function ($id) {
    var_dump($id);
});

OpenSwoole\Event::cycle(function () {

    echo "hello [1]\n";

    OpenSwoole\Event::cycle(function () {

        echo "hello [2]\n";

        // Clear all cycle functions
        OpenSwoole\Event::cycle(null);

    });

});
Last updated on September 1, 2022