Home >PHP Framework >Swoole >Why can't swoole use sleep?
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.
The operating environment of this tutorial: Windows 10 system, Swoole 4 version, DELL G3 computer
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!