Menu
Reference
- Manticore\Ds
- Async
- Classes
- AggregateError
- CancellationToken
- CancelledException
- Channel
- ChannelClosedException
- Context
- DeadlockException
- Mutex
- Once
- Pool
- Received
- SelectCase
- Selected
- Semaphore
- Settled
- Task
- TaskGroup
- TimeoutException
- Functions
- async()
- awaitAll()
- awaitAllSettled()
- awaitAny()
- channel()
- delay()
- dump()
- dumpOn()
- failure()
- group()
- mapConcurrent()
- mapSettled()
- readFile()
- select()
- selectNow()
- selectWithin()
- shield()
- shutdownOn()
- spawn()
- stats()
- timeout()
- watchdog()
- writeFile()
- Http
- Http\WebSocket
- Buffer
- Process
- Io\Poll
- Manticore\Sapi
- Ffi
channel()
function
function channel(int $capacity = 0): Channel
Make a channel.
Parameters
$capacityint = 0- buffer size; 0 (the default) is an unbuffered rendezvous
Returns
Channel — the new channel
Example
use Async\ChannelClosedException;
use function Async\async;
use function Async\channel;
use function Async\spawn;
async(function () {
// Capacity 0 is a rendezvous: send() waits for a receiver.
$ch = channel();
spawn(function () use ($ch) {
foreach ([1, 2, 3] as $n) {
$ch->send($n);
}
$ch->close();
});
// foreach ends once the channel is closed and drained.
foreach ($ch as $value) {
echo "got $value\n";
}
// next() is the explicit form: ok is false on a closed channel,
// which lets null be a legitimate payload.
$r = $ch->next();
echo $r->ok ? "value\n" : "closed\n";
try {
$ch->send(4);
} catch (ChannelClosedException $e) {
echo "send failed: ", $e->getMessage(), "\n";
}
// A buffered channel takes values without a waiting receiver.
$buf = channel(2);
$buf->send("a");
$buf->send("b");
echo $buf->recv(), $buf->recv(), "\n";
});Output
got 1
got 2
got 3
closed
send failed: send on a closed channel
abSee also: Channel