Method
phasync::readable
Suspends the coroutine until the stream can be read without blocking, and returns the stream.
Description#
public static phasync::readable(
mixed $resource,
float $timeout = PHP_FLOAT_MAX
): mixedThe stream is readable when data has arrived, the peer has finished, or the stream failed, so the read that follows does not wait. Set the stream to non-blocking with stream_set_blocking() first, or the read that follows blocks the process. Outside a coroutine it returns at once for a stream in blocking mode, and otherwise blocks until the stream is readable.
One coroutine at a time may wait to read a stream, while another waits to write to it.
Parameters#
- $resource mixed
- A stream
- $timeout float = PHP_FLOAT_MAX
- Seconds to wait at most
Return values#
$resource, for use in the call that reads it mixed
Errors / Exceptions#
| Exception | When |
|---|---|
IOException | If $resource is not an open stream, or is closed while waiting |
LogicException | If another coroutine is waiting to read $resource |
TimeoutException | If the stream is not readable in time |
Examples#
phasync::run(function () {
[$a, $b] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
stream_set_blocking($a, false);
phasync::go(function () use ($b) { phasync::sleep(0.1); fwrite($b, "hello"); });
echo fread(phasync::readable($a), 1024), "\n"; // hello, after 0.1 seconds
});See also#
phasync::writableSuspends the coroutine until the stream can be written without blocking, and returns the stream.