Swoole Coroutine System: sleep

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

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

Parameters

seconds

The duration to sleep for. The value is a int, so 2 means 2 seconds. No more than one day which is 86400 seconds.

Return

Returns true when successful and false when not.

Description

Since Open Swoole v4.10.0 the data type of $seconds is fixed inline with PHP

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::sleep(5);
    $response->end("<h1>Hello, from Swoole!</h1>");
});

$server->start();
Last updated on August 31, 2022