Home >PHP Framework >Swoole >Why can't swoole use sleep?

Why can't swoole use sleep?

WBOY
WBOYOriginal
2022-04-13 15:09:282734browse

Because in swoole, using the sleep function will cause the process to fall into sleep blocking; only signals can interrupt the sleep process. Since swoole's signal is based on signalfd, sleep cannot be interrupted even if a signal is sent. The operating system will not reawaken the current process until the specified time.

Why can't swoole use sleep?

The operating environment of this tutorial: Windows 10 system, Swoole 4 version, DELL G3 computer

Why swoole cannot be used with sleep

In In asynchronous IO programs, sleep/usleep/time_sleep_until/time_nanosleep must not be used. (sleep is used below to refer to all sleep functions)

  • The sleep function will cause the process to fall into sleep blocking

  • until the specified time has elapsed and the operating system Only then will the current process be reawakened

  • During the sleep process, only signals can interrupt

  • Since Swoole's signal processing is implemented based on signalfd , so sleep cannot be interrupted even if a signal is sent

The swoole_event_add, swoole_timer_tick, swoole_timer_after, swoole_process::signal, and asynchronous swoole_client provided by Swoole will stop working after the process sleeps. swoole_server can no longer handle new requests.

Instance program

$serv = new swoole_server("127.0.0.1", 9501);
$serv->set(['worker_num' => 1]);
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
    sleep(100);
    $serv->send($fd, 'Swoole: '.$data);
});
$serv->start();

The sleep function is executed in the onReceive event, and the server cannot receive any more client requests within 100 seconds.

Recommended learning: swoole tutorial

The above is the detailed content of Why can't swoole use sleep?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn