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,'print_line',[$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); }