Skip to content

Core\Task\Channel::send

$channel->send(T $value): void$channel->send(
    T $value,
): void

Puts one value into a channel, for another task running at the same time to take out.

A channel holds a fixed number of values, chosen when you create it. While it is full, send pauses the task that called it, and that task continues as soon as the other one takes a value out. This is how a fast producer is kept from running ahead of a slow consumer and filling up memory.

In plain words: a conveyor belt with a fixed number of slots. When every slot is taken, the worker filling them waits until one comes free.

Good to know: send on a channel that is already closed stops the program with an error, and no catch sees it. Close a channel after the last send and never before.

The examples below hand values to another task, show the sender waiting for a full channel, and put a producer and a worker together the way a program does.

Parameters

  • $valueTThe value to send.

Return value

Returns void. Nothing. send returns when the value is in the queue. A send on a closed channel is a fatal error, and a catch block cannot catch it.

Examples

Handing values to another task

<?nvs
// Shows two tasks working at the same time. One of them puts numbers into the
// channel, the other takes them out and prints them.
// The channel holds two values at a time.
var $chan = new Core\Task\Channel<int>(2);
Core\Task::all({
// `send` puts one value in. The channel is closed after the last one, so
// the loop below knows that nothing more is coming.
produce: fn(): int => {
foreach (Core\Arr::range(1, 4) as int $i) {
$chan->send($i * 10);
}
$chan->close();
return 0;
},
// A `foreach` over the channel takes the values out in the order they were
// sent. This prints 10, 20, 30 and 40.
consume: fn(): int => {
foreach ($chan as int $value) {
echo $value, "\n";
}
return 0;
},
});
Output
10
20
30
40

A full channel makes the sender wait

<?nvs
// Shows what a full channel does to the sender. This channel has room for one value.
// The sender sends four values. When the channel is full, `send` waits until the
// reader takes the value out. Then it puts the next value in.
class Meter
{
public static int $taken = 0;
public static int $waits = 0;
}
var $chan = new Core\Task\Channel<int>(1);
Core\Task::all({
produce: fn(): int => {
foreach (Core\Arr::range(1, 4) as int $i) {
// If the reader took a value while `send` ran, then `send` waited.
// This counts those sends. The program prints `3`.
int $before = Meter::$taken;
$chan->send($i);
if (Meter::$taken > $before) {
Meter::$waits += 1;
}
}
$chan->close();
return 0;
},
consume: fn(): int => {
foreach ($chan as int $value) {
Meter::$taken += 1;
}
return 0;
},
});
echo 'values taken: ', Meter::$taken, "\n";
echo 'sends that waited: ', Meter::$waits, "\n";
Output
values taken: 4
sends that waited: 3

A producer and a worker

<?nvs
// Shows the everyday use: one task produces work and another does it. The
// channel holds three prices, so the reader never runs far ahead of the till
// however long the list of orders gets.
class Till
{
public static int $total = 0;
}
var $orders = new Core\Task\Channel<int>(3);
Core\Task::all({
read: fn(): int => {
array<int> $prices = [1299, 450, 2000, 99, 1750];
foreach ($prices as int $price) {
$orders->send($price);
}
// Closing ends the worker's loop once the queued prices are gone.
$orders->close();
return 0;
},
sum: fn(): int => {
foreach ($orders as int $price) {
Till::$total += $price;
}
return Till::$total;
},
});
// This prints 5598.
echo 'total: ', Till::$total, "\n";
Output
total: 5598