forked from nikic/ditaio
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhello-parallel.php
64 lines (53 loc) · 1.78 KB
/
hello-parallel.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
<?php
include 'vendor/autoload.php';
use parallel\{Runtime, Channel};
main($argv);
function main(array $argv)
{
if (count($argv) !== 3) {
echo "Type: hello-parallel.php <number-of-tasks> <maximum-time-of-sleep (in seconds)>" . PHP_EOL;
echo "Example: hello-parallel.php 5 3" . PHP_EOL;
die;
} else {
$numberOfTasks = intval($argv[1]);
$maximumTimeOfSleep = intval($argv[2]);
$t1 = microtime(true);
parallelize($numberOfTasks, $maximumTimeOfSleep);
$endTime = microtime(true) - $t1;
echo PHP_EOL . "Finished $numberOfTasks task(s) in {$endTime}s" . PHP_EOL;
}
}
function parallelize(int $numberOfTasks, int $maximumTimeOfSleep)
{
$channel = new Channel();
$taskIds = array_map(function () use ($maximumTimeOfSleep) {
return $id = uniqid("task::");
}, range(0, $numberOfTasks - 1));
$timesToSleep = array_map(function () use ($maximumTimeOfSleep) {
return rand(1, $maximumTimeOfSleep);
}, $taskIds);
$producer = new Runtime();
$producerFuture = $producer->run(function (Channel $channel, array $timesToSleep) {
foreach ($timesToSleep as $timeToSleep) {
$channel->send($timeToSleep);
}
}, $channel, $timesToSleep);
$consumerFutures = array_map(function (string $id) use ($channel) {
$runtime = new Runtime();
return $runtime->run(function (string $id, Channel $channel) {
$timeToSleep = $channel->recv();
echo "Hello from $id. I will sleep for $timeToSleep second(s)." . PHP_EOL;
sleep($timeToSleep);
echo "$id slept for $timeToSleep second(s)." . PHP_EOL;
return $timeToSleep;
}, $id, $channel);
}, $taskIds);
wait($consumerFutures);
wait([$producerFuture]);
}
function wait(array $futures)
{
return array_map(function ($future) {
return $future->value();
}, $futures);
}