Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the reason why swoole cannot use sleep?

Shulou Source: shulou.com Published: 2022-05-31 22:52:03 10月02日 Update

This article mainly introduces "what is the reason why swoole can not use sleep". In daily operation, I believe that many people have doubts about the reason why swoole cannot use sleep. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt that "swoole can not use sleep". Next, please follow the editor to study!

Because in swoole, using the sleep function will cause the process to fall into sleep blocking; only the signal in the sleep process can be interrupted, because the signal of swoole is based on signalfd, so even if the signal is sent, the sleep cannot be interrupted, and only after the specified time will the operating system wake up the current process again.

Operating environment of this tutorial: Windows10 system, Swoole4 version, DELL G3 computer

Why can't swoole use sleep?

Sleep/usleep/time_sleep_until/time_nanosleep must not be used in asynchronous IO programs. (the use of sleep below generally refers to all sleep functions)

The sleep function causes the process to fall into a sleep block

The operating system will not reawaken the current process until the specified time

During sleep, only the signal can interrupt.

Since the signal processing of Swoole is based on signalfd, the sleep cannot be interrupted even if the signal is sent.

Swoole_event_add, swoole_timer_tick, swoole_timer_after, swoole_process::signal, and asynchronous swoole _ client provided by Swoole stop working after the process sleep. Swoole_server can no longer process new requests.

Example 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 server cannot receive any more client requests within 100 seconds.

At this point, the study on "what is the reason why swoole cannot use sleep" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Signals processes causes functions learning only systems sleep operating systems time more programs procedures processing help blocking utility and then below events Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS Redmi Linux Huawei