Example about Channel
Example:
<?php
declare(ticks=1);
use vennv\vapm\CoroutineGen;
use vennv\vapm\Channel;
$channel = new Channel();
CoroutineGen::runNonBlocking(function() use (&$channel) {
yield from $channel->receiveGen(function ($result) use (&$channel) {
echo "A: $result\n";
});
});
CoroutineGen::runNonBlocking(function() use (&$channel) {
yield from $channel->receiveGen(function ($result) {
echo "B: $result\n";
});
});
CoroutineGen::runNonBlocking(function() use (&$channel) {
for ($i = 0; $i < 10; $i++) {
yield from $channel->sendGen($i);
}
$channel->close(); // You need must close the channel after sending all the messages
});
Output:
A: 0
B: 1
A: 2
B: 3
A: 4
B: 5
A: 6
B: 7
A: 8
B: 9
Another example:
<?php
declare(ticks=1);
use vennv\vapm\Channel;
use vennv\vapm\CoroutineGen;
$channel = new Channel();
CoroutineGen::runNonBlocking(
function () use (&$channel) {
for ($i = 0; $i < 10; $i++) {
yield from $channel->sendGen($i);
}
$channel->close();
},
);
$channel->receive(function ($result) {
echo "Channel: $result\n";
});
Output:
Channel: 0
Channel: 1
Channel: 2
Channel: 3
Channel: 4
Channel: 5
Channel: 6
Channel: 7
Channel: 8
Channel: 9
Last updated