Swoole Coroutine System: sleep

Declaration

<?php
Swoole\Coroutine\System::sleep(float $seconds): void

Parameters

seconds

The duration to sleep for. The value is a float, so 2.5 means 2.5 seconds. Minimum precision is 0.001 and the maximum is no more than one day which is 86400 seconds.

Return

Returns true when successful and false when not.

Description

Enter a sleep state within a coroutine and release the CPU to do other operations in the process. This is a non-blocking operation, the coroutine yields so that other coroutines can do work until the sleep has finished.

Note: The native PHP sleep() function should not be used within a coroutine context without coroutine hooks. However, if you have enabled coroutine hooks the recommended approach is to then only use the native sleep function. Checkout the sleep hook for more information, this allows you to use native PHP functions inside coroutines.


Example

<?php

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

$server->on('Request', function($request, $response)
{
    Co\System::sleep(5.5);
    $response->end("<h1>Hello, from Swoole!</h1>");
});

$server->start();