-
Notifications
You must be signed in to change notification settings - Fork 1
/
loop_cancelling_timers.php
48 lines (39 loc) · 1.78 KB
/
loop_cancelling_timers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* ---------------------------------------------------------------------------------------------------------------------
* DESCRIPTION
* ---------------------------------------------------------------------------------------------------------------------
* This file contains the example of cancelling scheduled timers in loop using alternative way.
*
* ---------------------------------------------------------------------------------------------------------------------
* USAGE
* ---------------------------------------------------------------------------------------------------------------------
* To run this example in CLI from project root use following syntax:
*
* $> php ./example/loop_cancelling_timers_alternative.php
*
* Following flags are supported to test example with different configurations:
*
* --model : define model to use, default: select, supported: [ select ]
*
* Ex:
* $> php ./example/loop_cancelling_timers_alternative.php --model=select
*
* ---------------------------------------------------------------------------------------------------------------------
*/
$model = '';
require_once __DIR__ . '/bootstrap/autoload.php';
use Dazzle\Loop\Loop;
$loop = new Loop(new $model);
$timer1 = $loop->addTimer(1, function() {
printf("This tick should be executed once with 1s delay.\n");
});
$timer2 = $loop->addPeriodicTimer(0.1, function() {
printf("This tick should be executed repeatedly with 0.1s delay.\n");
});
$loop->addTimer(0.6, function() use($loop, $timer1, $timer2) {
$loop->cancelTimer($timer1); // this will cancel $timer1 before it could be executed
$loop->cancelTimer($timer2); // this will cancel $timer2 after ~5 executions
printf("Cancellation has been called on timers, nothing more will be printed!\n");
});
$loop->start();