Channels
A channel is a queue between tasks. One task puts values in with send. Another task takes them
out with foreach.
Core\Task\Channel
A Core\Task\Channel<T> is a queue that one task fills and another empties. You give it a capacity when you make it, and it holds that many values. send puts one value in. If the channel is full, send waits until there is room, so the queue never grows.
You read a channel with a foreach loop. It takes the values out one at a time, in the order they were sent. The loop waits while the channel is empty, and ends once it is empty and closed.
close says that no more values are coming. Values already in the queue are still delivered, and closing twice does nothing. A task that never closes leaves the reading task waiting forever. Sending into a closed channel stops the program with an error, and so does a capacity of 0.
The examples below show two tasks passing values, what the capacity does, and orders billed while they are still being read.
Two tasks pass values through a queue
<?nvs// Shows how one task sends values to another through a channel. `close` tells the// reading task that no more values are coming.
var $lines = new Core\Task\Channel<string>(4);
Core\Task::all({ write: fn(): void => { $lines->send('first'); $lines->send('second'); $lines->send('third'); // Close the channel when you are done sending. Without this the reading // task waits forever. $lines->close(); }, read: fn(): void => { // The loop takes one value at a time. It ends when the channel is empty // and closed. foreach ($lines as string $line) { echo $line, "\n"; } echo "no more lines\n"; },});firstsecondthirdno more linesThe queue holds a fixed number of values
<?nvs// Shows what the capacity of a channel does. This one holds 2 values. `send` waits// while the channel is full, so the queue never holds more than 2 values. The memory// it uses is the same for 8 values and for 8 million.
var $jobs = new Core\Task\Channel<int>(2);
var $done = Core\Task::all({ fill: fn(): int => { foreach (Core\Arr::range(1, 8) as int $n) { $jobs->send($n); } $jobs->close(); return 8; }, work: fn(): int => { int $sum = 0; foreach ($jobs as int $n) { echo 'handled job ', $n, "\n"; $sum = $sum + $n; } return $sum; },});
// This prints 8 and 36.echo $done->fill, ' jobs, total ', $done->work, "\n";handled job 1handled job 2handled job 3handled job 4handled job 5handled job 6handled job 7handled job 88 jobs, total 36Orders are billed while they are still being read
<?nvs// Shows a real use: one task reads orders and another writes a receipt for each one.// The channel holds 2 orders, so the reading task waits for the writer. A list of a// million orders uses the same memory as a list of three.
class Order{ public function constructor(public string $id, public int $cents) {}}
class Shop{ public static function orders(): array<Order> { return [ new Order('A-1001', 1250), new Order('A-1002', 900), new Order('A-1003', 4300), ]; }}
var $queue = new Core\Task\Channel<Order>(2);
var $till = Core\Task::all({ read: fn(): int => { int $count = 0; foreach (Shop::orders() as Order $order) { $queue->send($order); $count = $count + 1; } $queue->close(); return $count; }, bill: fn(): int => { int $cents = 0; foreach ($queue as Order $order) { echo 'receipt ', $order->id, ': ', $order->cents, " cents\n"; $cents = $cents + $order->cents; } return $cents; },});
// This prints 3 and 6450.echo $till->read, ' orders, ', $till->bill, " cents in total\n";receipt A-1001: 1250 centsreceipt A-1002: 900 centsreceipt A-1003: 4300 cents3 orders, 6450 cents in totalDetails
The capacity is a uint. -1 and an int variable are E0401. A capacity of 0 stops the program with FATAL: Core\Task\Channel's constructor expected a capacity of at least 1, got 0 and exit status 1.
You must write the value type. new Core\Task\Channel(1) is E0442. A send of another type is E0401, and so is a foreach variable of another type. foreach ($chan as var $v) gives $v the channel's value type. A key, as in foreach ($chan as $k => int $v), is E0444.
A channel has the methods send and close. There is no receive method (E0405). A foreach loop is the way to take values out.
A send on a closed channel stops the program with FATAL: Core\Task\Channel::send was called on a closed channel and exit status 1. No catch block and no finally block runs.
Nothing checks that another task will read or send. A send on a full channel with no reader waits forever. So does a foreach on an empty channel that nobody closes. This is true inside a task group and outside one. A deadline on the group stops that wait.
A second foreach over a channel that is closed and empty runs nothing.
An object you send is the same object in the reading task. A change the reader makes to it is seen by the sender.
You can pass a channel to a method. The parameter type is Core\Task\Channel<int>, with the value type.
Do and don’t
Section titled “Do and don’t”- Do call
closewhen the sending task is done. Without it, the loop waits forever. - Do pick a small capacity. A full channel slows the sender down, so the queue stays small.
- Don’t send after
close. It stops the whole program.
