-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.php
More file actions
51 lines (38 loc) · 1.44 KB
/
json.php
File metadata and controls
51 lines (38 loc) · 1.44 KB
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
<?php
declare(strict_types=1);
use React\EventLoop\Loop;
use ReactParallel\EventLoop\EventLoopBridge;
use ReactParallel\Pool\Infinite\Infinite;
use function React\Async\async;
use function React\Async\await;
use function React\Promise\all;
$json = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'large.json');
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$infinite = new Infinite(new EventLoopBridge(), 1);
Loop::futureTick(async(static function () use ($infinite, $json): void {
$promises = [];
$signalHandler = static function () use ($infinite): void {
Loop::stop();
$infinite->close();
};
$tick = async(static function () use (&$promises, $infinite, $signalHandler, $json, &$tick): void {
if (count($promises) < 1000) {
$promises[] = async(static fn (string $json): string => $infinite->run(static function ($json): string {
$json = json_decode($json, true);
return md5(json_encode($json));
}, [$json]))($json);
Loop::futureTick($tick);
return;
}
try {
var_export(await(all($promises)));
} finally {
$infinite->close();
Loop::removeSignal(SIGINT, $signalHandler);
Loop::stop();
}
});
Loop::futureTick($tick);
Loop::addSignal(SIGINT, $signalHandler);
}));
echo 'Loop::run()', PHP_EOL;