Channel::recv
(PHP 8.6+, True Async 1.0)
php
public Channel::recv(?Completable $cancellationToken = null): mixedReceives the next value from the channel. This is a blocking operation — the current coroutine is suspended if no values are available in the channel.
If the channel is closed and the buffer is empty, a ChannelException is thrown. If the channel is closed but values remain in the buffer, they will be returned.
Parameters
cancellationToken : A cancellation token (Completable) that allows cancelling the wait on any condition. null — wait indefinitely (default). When the token completes, the operation is cancelled and a AsyncCancellation is thrown. To limit by time, you can use Async\timeout().
Return values
The next value from the channel (mixed).
Errors
- Throws
Async\ChannelExceptionif the channel is closed and the buffer is empty. - Throws
Async\AsyncCancellationif the cancellation token has been completed.
Examples
Example #1 Receiving values from a channel
php
<?php
use Async\Channel;
$channel = new Channel(5);
spawn(function() use ($channel) {
for ($i = 1; $i <= 5; $i++) {
$channel->send($i);
}
$channel->close();
});
spawn(function() use ($channel) {
try {
while (true) {
$value = $channel->recv();
echo "Received: $value\n";
}
} catch (\Async\ChannelException) {
echo "Channel closed and empty\n";
}
});Example #2 Receiving with a timeout
php
<?php
use Async\Channel;
$channel = new Channel();
spawn(function() use ($channel) {
try {
$value = $channel->recv(Async\timeout(2000));
echo "Received: $value\n";
} catch (\Async\AsyncCancellation) {
echo "No data received within 2 seconds\n";
}
});Example #3 Receiving with a custom cancellation token
php
<?php
use Async\Channel;
use Async\Future;
$channel = new Channel();
$cancel = new Future();
spawn(function() use ($channel, $cancel) {
try {
$value = $channel->recv($cancel);
echo "Received: $value\n";
} catch (\Async\AsyncCancellation) {
echo "Receive cancelled\n";
}
});
// Cancel from another coroutine
spawn(function() use ($cancel) {
Async\delay(500);
$cancel->complete(null);
});See also
- Channel::recvAsync — Non-blocking receive
- Channel::send — Send a value to the channel
- Channel::isEmpty — Check if the buffer is empty
- Channel::getIterator — Iterate over the channel using foreach