fiber-interop

fiber\readable()

Wait until a stream is readable and return it.

function fiber\readable(mixed $resource, float $timeout = PHP_FLOAT_MAX): mixed

Behaviour

Returns the resource so that fread(readable($fp), 8192) works. Several fibers may wait on the same resource at once. Throws TimeoutException on timeout and NotInRunException outside run(). Put the stream in non-blocking mode first, as the example does.

Example

Source: examples/api/readable.php. Run on phasync and Revolt; the output is identical.

<?php
require __DIR__ . '/vendor/autoload.php';
use function fiber\{run, go, await, sleep, readable};
use fiber\TimeoutException;
run(function () {
    [$r, $w] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
    stream_set_blocking($r, false);
    try {
        readable($r, 0.05);
    } catch (TimeoutException) {
        echo "nothing to read yet\n";
    }
    go(function () use ($w) { sleep(0.05); fwrite($w, "hello"); });
    echo fread(readable($r, 1.0), 8192), "\n";
});

All functions