This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channels.php
64 lines (53 loc) · 1.56 KB
/
channels.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
require 'vendor/autoload.php';
use \parallel\Runtime;
use \parallel\Channel;
use \parallel\Future;
use \parallel\Events;
use \parallel\Events\Event;
use \parallel\Events\Input;
use \parallel\Events\Timeout;
use React\EventLoop\Factory;
$parallel = new \parallel\Runtime();
$channel = Channel::make("channel", Channel::Infinite);
$input = new Input();
$input->add("channel", "input");
$events = new Events();
$events->setTimeout(1);
$events->setInput($input);
$events->addChannel($channel);
$events->addFuture("future", $parallel->run(function(){
sleep(1);
return 42;
}));
$loop = Factory::create();
$loop->addPeriodicTimer(0.001, function () use ($events, $channel, $loop, &$timer) {
try {
$event = $events->poll();
} catch (Timeout $timeout) {
echo microtime(true), 'timeout', PHP_EOL;
return;
}
echo microtime(true), 'tick', PHP_EOL;
if (!($event instanceof Event)) {
return;
}
switch ($event->type) {
case Event::Read:
echo microtime(true), 'read', PHP_EOL;
if ($event->object instanceof Future &&
$event->value == 42) {
echo microtime(true), "OK:Future:" . $event->value . "\n";
}
if ($event->object instanceof Channel &&
$event->value == "input") {
echo microtime(true), "OK:Channel\n";
}
break;
case Event::Write:
echo microtime(true), 'write', PHP_EOL;
$events->addChannel($channel);
break;
}
});
$loop->run();