-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.php
96 lines (81 loc) · 2.02 KB
/
example.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/** @noinspection PhpComposerExtensionStubsInspection */
declare(strict_types=1);
namespace bin;
include __DIR__ . '/../vendor/autoload.php';
use Async\AsyncCall;
use Exception;
use RuntimeException;
$s = microtime(true);
AsyncCall::run(
static function () {
sleep(2);
// there is no callback so echo will not be showed and parent process will not w8
echo 'sleep 2s' . PHP_EOL;
}
);
AsyncCall::run(
static function () {
sleep(4);
// echo will be catched in child process and returned as second parameter $stdOut
echo 'sleep 4s' . PHP_EOL;
},
static function ($results, $stdOut) {
echo $stdOut;
}
);
AsyncCall::run(
static function () {
throw new RuntimeException('bar');
}
);
AsyncCall::run(
static function () {
throw new RuntimeException('foo');
},
static function () {
},
static function (Exception $error) {
// we will get error
assert($error->getMessage() === 'foo');
}
);
AsyncCall::run(
static function () {
// if this is in parent, child will not see this
function getPage($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
// this will be returned to callback as first parameter
return getPage('example.com');
},
static function ($results) {
echo $results;
}
);
// -------- process limit ----------
// set GLOBAL limit for process
AsyncCall::setProcessLimit(2);
$i = 10;
while ($i--) {
// this will start 2 process and then wait them to finish before starting second one
AsyncCall::run(
static function () {
sleep(1);
},
static function () {
}
);
}
// reset limit
AsyncCall::setProcessLimit(0);
echo PHP_EOL;
echo 'Script ended: ';
echo microtime(true) - $s;
echo PHP_EOL;