2017 © Pedro Peláez
 

library wait-condition-loop

Wait loop that reaches a condition or times out

image

wikimedia/wait-condition-loop

Wait loop that reaches a condition or times out

  • Saturday, July 28, 2018
  • by mediawiki
  • Repository
  • 10 Watchers
  • 1 Stars
  • 80,784 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 11 % Grown

The README.md

![Latest Stable Version] ![License], (*1)

Wait Condition Loop for PHP

This class is used for waiting on a condition to be reached, with the ability to specify a timeout. The condition is considered reached when the condition callback returns CONDITION_REACHED or true. CONDITION_ABORTED can also be used to stop the loop., (*2)

Additionally, "work" callbacks can be injected to prepare useful work instead of simply having the current thread sleep or block on I/O. The loop will run one of these callbacks on each iteration of checking the condition callback, as long as there are any left to run., (*3)

The loop class will automatically either retry the condition or usleep() before retrying it, depending on CPU usage. Low CPU usage and significant real-time passage is used to detect whether the condition callback appears to use blocking I/O. Use of usleep() will not occur until all of the "work" callbacks have run. This means that the condition callback can either be an "instant" CPU-bound check or a blocking I/O call with a small timeout. Both cases should automatically work without CPU intensive spin loops., (*4)

Additional documentation about the library can be found on mediawiki.org., (*5)

Usage

// Pre-compute some value that will be needed later
$result = null;
$workCallback = function () use ( &$result ) {
    $result = ( $result !== null ) ? $result : $this->doWork();

    return $result
}

$loop = new WaitConditionLoop(
    function () use ( ... ) {
        if ( ... ) {
            // Condition reached; stop loop
            return WaitConditionLoop::CONDITION_REACHED;
        }
        // Condition not reached; keep checking
        return WaitConditionLoop::CONDITION_CONTINUE;
    },
    3.0, // timeout in seconds
    [ $workCallback ]
);
$status = $loop->invoke(); // CONDITION_* constant

// Call $workCallback as needed later

Running tests

composer install
composer test

The Versions

01/10 2016

v1.0.1

1.0.1.0 https://www.mediawiki.org/wiki/WaitConditionLoop

Wait loop that reaches a condition or times out

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.5.9

 

The Development Requires

by Aaron Schulz

30/09 2016

v1.0.0

1.0.0.0 https://www.mediawiki.org/wiki/WaitConditionLoop

Wait loop that reaches a condition or times out

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.5.9

 

The Development Requires

by Aaron Schulz