Swoole\Coroutine\System::waitEvent(mixed $resource, int $events = SWOOLE_EVENT_READ, float $timeout = -1): int | false

Declaration

<?php
Swoole\Coroutine\System::waitEvent(mixed $resource, int $events = SWOOLE_EVENT_READ, float $timeout = -1): int | false

Parameters

resource

Resource such as a socket or fd, sockets, php_stream, Swoole\Client, Swoole\Process, Swoole\Coroutine\Client, fd.

events

SWOOLE_EVENT_READ, SWOOLE_EVENT_WRITE, or SWOOLE_EVENT_WRITE | SWOOLE_EVENT_READ

timeout

Waiting timeout.

Return

Description

Wait for the I\O event witin coroutine context.

Example

<?php
Co\run(function () {
    $stdin = fopen("php://stdin", 'r');
    $ip = Swoole\Coroutine\System::waitEvent($stdin, SWOOLE_EVENT_READ, 5);
    if ($ip) {
      echo fgets($stdin);
    } else {
      echo "timeout\n";
    }
});