forked from nikic/ditaio
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhello_cancel.php
48 lines (40 loc) · 1.03 KB
/
hello_cancel.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
/**
* From Curio: A Tutorial Introduction
* @see https://curio.readthedocs.io/en/latest/tutorial.html#tasks-and-concurrency
*/
include 'vendor/autoload.php';
async('countdown', function ($n) {
while ($n > 0) {
print('T-minus ' . $n . EOL);
yield await('sleep', 1);
$n--;
}
});
async('kid', function ($x, $y) {
try {
yield print('Getting around to doing my homework' . EOL);
yield await(sleep, 1000);
return $x * $y;
} catch (Async\CancelledError $e) {
print("No go diggy die!" . EOL);
// yield raise();
// --Same as--
// throw $e;
yield shutdown();
}
});
async('parent', function () {
$kid_task = yield spawner(kid, 37, 42);
$count_task = yield spawner(countdown, 10);
yield join_task($count_task);
print("Are you done yet?" . EOL);
try {
$result = yield timeout_after(10, join_task($kid_task));
print("Result: " . $result);
} catch (Async\TaskTimeout $e) {
print("We've got to go!" . EOL);
yield cancel_task($kid_task);
}
});
coroutine_run(parent);