]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/wikimedia/wait-condition-loop/README.md
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / wikimedia / wait-condition-loop / README.md
1 [![Latest Stable Version]](https://packagist.org/packages/wikimedia/wait-condition-loop) [![License]](https://packagist.org/packages/wikimedia/wait-condition-loop)
2
3 Wait Condition Loop for PHP
4 ===========================
5
6 This class is used for waiting on a condition to be reached, with the ability
7 to specify a timeout. The condition is considered reached when the condition callback
8 returns CONDITION_REACHED or true. CONDITION_ABORTED can also be used to stop the loop.
9
10 Additionally, "work" callbacks can be injected to prepare useful work instead of simply
11 having the current thread sleep or block on I/O. The loop will run one of these callbacks
12 on each iteration of checking the condition callback, as long as there are any left to run.
13
14 The loop class will automatically either retry the condition or usleep() before retrying it,
15 depending on CPU usage. Low CPU usage and significant real-time passage is used to detect
16 whether the condition callback appears to use blocking I/O. Use of usleep() will not occur
17 until all of the "work" callbacks have run. This means that the condition callback can
18 either be an "instant" CPU-bound check or a blocking I/O call with a small timeout. Both
19 cases should automatically work without CPU intensive spin loops.
20
21 Additional documentation about the library can be found on
22 [MediaWiki.org](https://www.mediawiki.org/wiki/WaitConditionLoop).
23
24
25 Usage
26 -----
27         // Pre-compute some value that will be needed later
28         $result = null;
29         $workCallback = function () use ( &$result ) {
30                 $result = ( $result !== null ) ? $result : $this->doWork();
31
32                 return $result
33         }
34
35         $loop = new WaitConditionLoop(
36                 function () use ( ... ) {
37                         if ( ... ) {
38                                 // Condition reached; stop loop
39                                 return WaitConditionLoop::CONDITION_REACHED;
40                         }
41                         // Condition not reached; keep checking
42                         return WaitConditionLoop::CONDITION_CONTINUE;
43                 },
44                 3.0, // timeout in seconds
45                 [ $workCallback ]
46         );
47         $status = $loop->invoke(); // CONDITION_* constant
48
49         // Call $workCallback as needed later
50
51 Running tests
52 -------------
53
54     composer install --prefer-dist
55     composer test
56
57
58 ---
59 [Latest Stable Version]: https://poser.pugx.org/wikimedia/wait-condition-loop/v/stable.svg
60 [License]: https://poser.pugx.org/wikimedia/wait-condition-loop/license.svg