search
HomePHP FrameworkSwooleWhy 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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools