Method
ReadChannelInterface::read
Returns the next value, and waits while there is none and the channel is open.
Description#
public ReadChannelInterface::read(
float $timeout = PHP_FLOAT_MAX,
?bool &$eof = null
): mixednull is both a value that can be written and what read() returns when the channel is closed with nothing left, so the return value alone cannot tell them apart. Pass $eof: it is set to true only when the channel is closed and empty, and false otherwise, also when the value is null.
Parameters#
- $timeout float = PHP_FLOAT_MAX
- Seconds to wait at most
- $eof ?bool = null
- Set to true if the channel is closed and empty
Return values#
The value, or null at the end of the channel mixed
Errors / Exceptions#
| Exception | When |
|---|---|
TimeoutException | If nothing arrived in time |
ChannelException | If the creator of the channel waits on it alone, see ReadChannelInterface::activate |
Examples#
phasync::run(function () {
phasync::channel($read, $write, 2);
$write->write(null);
$write->close();
$read->read(eof: $eof); // null, $eof is false: a value
$read->read(eof: $eof); // null, $eof is true: the end
});See also#
WriteChannelInterface::writeSends a value, and waits while the channel cannot take it: for a reader on an unbuffered channel, for room on a full buffer.