Interface
ReadChannelInterface
The reading end of a channel: values written to the channel arrive here in the order they were written.
Introduction#
Any PHP value can be sent, because a channel is in-memory communication between coroutines of one process and nothing is serialized. read() waits while the channel is empty, foreach over the end reads until the channel is closed and empty, and each value goes to exactly one reader. Closing either end closes the channel, and so does dropping the last reference to either end; values already written can still be read.
Class synopsis#
interface ReadChannelInterface extends SelectableInterface, \Traversable {
/* Methods */
public ReadChannelInterface::activate(): void
public ReadChannelInterface::close(): void
public ReadChannelInterface::isClosed(): bool
public ReadChannelInterface::read(
float $timeout = PHP_FLOAT_MAX,
?bool &$eof = null
): mixed
public ReadChannelInterface::isReadable(): bool
}Methods#
| Method | Description |
|---|---|
activate | Switches off the deadlock check of the channel. |
close | Closes the channel, for both ends. |
isClosed | Returns true if the channel has been closed, also when values are left to read. |
read | Returns the next value, and waits while there is none and the channel is open. |
isReadable | Returns true if the channel is open, or has values left to read. |
Examples#
phasync::run(function () {
phasync::channel($read, $write);
phasync::go(function () use ($write) {
$write->write('hello');
$write->close();
});
foreach ($read as $message) {
echo $message, "\n"; // hello
}
});See also#
phasync::channelCreates a channel and returns its two ends in$readand$write.WriteChannelInterfaceThe writing end of a channel:write()sends a value to the readers of the channel.SubscriberInterfaceWhen every reader should get every value