定制 wikimedia/wait-condition-loop 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wikimedia/wait-condition-loop

最新稳定版本:v2.0.2

Composer 安装命令:

composer require wikimedia/wait-condition-loop

包简介

Wait loop that reaches a condition or times out

README 文档

README

Latest Stable Version License

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.

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.

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.

Additional documentation about the library can be found on mediawiki.org.

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

统计信息

  • 总下载量: 672.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 9
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2016-09-30