phasync
phasync / writable

Method

phasync::writable

Suspends the coroutine until the stream can be written without blocking, and returns the stream.

Description#

public static phasync::writable(
    mixed $resource,
    float $timeout = PHP_FLOAT_MAX
): mixed

As phasync::readable does for reading. One coroutine at a time may wait to write to a stream, while another waits to read from 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 writes it mixed

Errors / Exceptions#

ExceptionWhen
IOExceptionIf $resource is not an open stream, or is closed while waiting
LogicExceptionIf another coroutine is waiting to write to $resource
TimeoutExceptionIf the stream is not writable in time

Examples#

Example 1
stream_set_blocking($socket, false);
$written = fwrite(phasync::writable($socket), $data);   // may write less than all of $data

See also#

  • phasync::readableSuspends the coroutine until the stream can be read without blocking, and returns the stream.