Home > php教程 > PHP源码 > body text

About the sustainability of events

大家讲道理
Release: 2016-11-11 09:23:28
Original
1369 people have browsed it

About event persistence
By default, any time a pending event is activated (because its fd is ready for reading or writing, or because its timeout has expired), it will become Not pending. If you want the event to be pending again, you need to call event_add() inside the callback function.
If an event is set to EV_PERSIST, then the event is persistent, which means that the event will remain pending even if the callback function is executed. If you want it to become non-suspended, you can call event_del() in the callback function.

Any time the callback function of the event is triggered, the timeout status in the persistent event will be reset. Therefore, if the event has EV_READ/EV_PERSIST and a 5-second timeout is set, then there are two situations that will trigger this event:

  • When the socket can be read

  • When the 5s timeout expires

<?php
$base = event_base_new();
$event = event_new();

event_set($event,STDIN,EV_READ | EV_PERSIST,&#39;print_line&#39;,[$event,$base]);

event_base_set($event,$base);
event_add($event,5000000);
event_base_loop($base);

function print_line($fd, $events, $arg)
{
    // 5秒超时会自动输出1,每次执行了read后,超时会被重置
    echo 1;
    static $max_requests = 0;
    $max_requests++;
    if ($max_requests == 10) {
        // $arg[1] = $base
        event_base_loopexit($arg[1]);
    }
    // 打印输出
    echo  fgets($fd);
}
Copy after login

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!