wikimedia/wait-condition-loop
Composer 安装命令:
composer require wikimedia/wait-condition-loop
包简介
Wait loop that reaches a condition or times out
README 文档
README
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
wikimedia/wait-condition-loop 适用场景与选型建议
wikimedia/wait-condition-loop 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 729.64k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2016 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wikimedia/wait-condition-loop 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wikimedia/wait-condition-loop 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 729.64k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 31
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2016-09-30