phasync
Channels and messaging / ReadChannelInterface

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#

MethodDescription
activateSwitches off the deadlock check of the channel.
closeCloses the channel, for both ends.
isClosedReturns true if the channel has been closed, also when values are left to read.
readReturns the next value, and waits while there is none and the channel is open.
isReadableReturns true if the channel is open, or has values left to read.

Examples#

Example 1
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#