Skip to content

Core\Task\Channel::close

$channel->close(): void

Ends the stream, so that the task reading the channel knows nothing more is coming.

A foreach over a channel waits while the channel is empty, because the next value may still be on its way. close is what tells the reader otherwise: the loop takes the values that are already queued and then ends. A reader waiting on a channel nobody will send to again waits forever, so the task that sends is the task that closes.

Good to know: closing twice changes nothing, so a program may close on every path out of its sending code. Sending after the close is different. That stops the program with an error, and no catch sees it.

The examples below end a reader's loop, show that the values queued before the close still arrive, and close on every path out of a producer.

Return value

Returns void. Nothing. Values sent before close are still received. Calling close twice does nothing.

Examples

Ending the readers loop

<?nvs
// Shows how a reading loop ends. The task that sends calls `close`, and the
// `foreach` in the other task finishes instead of waiting for more values.
var $chan = new Core\Task\Channel<int>(2);
Core\Task::all({
produce: fn(): int => {
foreach (Core\Arr::range(1, 3) as int $i) {
$chan->send($i);
}
// Without this line the loop below would wait for a fourth value that
// nobody is going to send.
$chan->close();
return 0;
},
consume: fn(): int => {
foreach ($chan as int $value) {
echo 'got ', $value, "\n";
}
// This line is reached because the channel was closed.
echo "the channel is closed\n";
return 0;
},
});
Output
got 1
got 2
got 3
the channel is closed

The queued values still arrive

<?nvs
// Shows that `close` does not throw away what is already in the channel. All
// five values are sent and the channel is closed before the reader starts, and
// the reader still gets every one of them.
class Tally
{
public static int $seen = 0;
}
var $chan = new Core\Task\Channel<int>(5);
Core\Task::all({
produce: fn(): int => {
foreach (Core\Arr::range(1, 5) as int $i) {
$chan->send($i);
}
$chan->close();
// Closing twice changes nothing.
$chan->close();
return 0;
},
consume: fn(): int => {
foreach ($chan as int $value) {
Tally::$seen += 1;
}
return 0;
},
});
// This prints 5.
echo 'values read after the close: ', Tally::$seen, "\n";
Output
values read after the close: 5

Closing on every path

<?nvs
// Shows a producer that can stop early. It closes the channel before each `return`.
// The `foreach` that reads the channel ends only when the channel is closed.
// If the channel is never closed, the reader waits forever.
class Report
{
public static int $rows = 0;
}
var $lines = new Core\Task\Channel<string>(2);
Core\Task::all({
read: fn(): int => {
array<string> $input = ['alice,3', 'bob,5', '', 'carol,2'];
foreach ($input as string $line) {
if ($line == '') {
// The empty line ends the input. The channel is closed here too.
$lines->close();
return 0;
}
$lines->send($line);
}
$lines->close();
return 0;
},
count: fn(): int => {
foreach ($lines as string $line) {
Report::$rows += 1;
}
return 0;
},
});
// This prints 2, because the empty third line ended the input.
echo 'rows: ', Report::$rows, "\n";
Output
rows: 2