phasync
Waiting and timeouts / SelectableInterface

Interface

SelectableInterface

An object that can be asked whether using it would block, and waited for until it would not.

Introduction#

isReady() is a non-blocking check. await() waits for it in the current coroutine, and phasync::await() takes any selectable. Channel ends, WaitGroup, StringBuffer and RateLimiter are selectable.

Class synopsis#

interface SelectableInterface {
    /* Methods */
    public SelectableInterface::await(float $timeout = PHP_FLOAT_MAX): void
    public SelectableInterface::isReady(): bool
}

Methods#

MethodDescription
awaitWaits until the object is ready: using it would not block.
isReadyReturns true when using the object would not block, for example because data is available or the source is closed or failed.

Examples#

Example 1
phasync::run(function () {
    $group = new phasync\Util\WaitGroup();
    $group->add();
    phasync::go(function () use ($group) { phasync::sleep(0.01); $group->done(); });

    var_dump($group->isReady());   // false: one task is left
    $group->await();
    var_dump($group->isReady());   // true
});

See also#

  • phasync::awaitWaits for a coroutine or a promise, and returns its result or throws the exception it ended with.
  • WaitGroupWaits until a counted amount of work has been done, like Go's sync.WaitGroup.