Coroutine::sleep

进入等待状态。相当于PHP的sleep函数,不同的是Coroutine::sleep是协程调度器实现的,底层会yield当前协程,让出时间片,并添加一个异步定时器,当超时时间到达时重新resume当前协程,恢复运行。使用sleep接口可以方便地实现超时等待功能。

function Coroutine::sleep(float $seconds);

2.0.9或更高版本可用

使用实例

$serv = new Swoole\Http\Server("127.0.0.1", 9502);

$serv->on('Request', function($request, $response) {
	//等待200ms后向浏览器发送响应
	Swoole\Coroutine::sleep(0.2);
    $response->end("<h1>Hello Swoole!</h1>");
});

$serv->start();