從網上找的內容,能夠看下github中的一本book,點擊下面的鏈接就ok了。
github-libevent_book引用其中的話php
About Event Persistence
By default, whenever a pending event becomes active (because its fd is ready to read or write, or because its timeout expires), it becomes non-pending right before its callback is executed. Thus, if you want to make the event pending again, you can call event_add() on it again from inside the callback function.
If the EV_PERSIST flag is set on an event, however, the event is persistent. This means that event remains pending even when its callback is activated. If you want to make it non-pending from within its callback, you can call event_del() on it.
The timeout on a persistent event resets whenever the event’s callback runs. Thus, if you have an event with flags EV_READ|EV_PERSIST and a timeout of five seconds, the event will become active:git
Whenever the socket is ready for reading.github
Whenever five seconds have passed since the event last became active.socket
可能有不對的地方,歡迎指正ide
關於事件的持續化
默認狀況下,任什麼時候候一個掛起的事件被激活(由於他的fd準備好了讀或者寫,或者由於他的超時過時了),它會在回調函數執行以前變爲非掛起。若是你想讓事件再次掛起,你須要在回調函數內部調用event_add()
。
若是一個事件被設置了EV_PERSIST
,那麼這個事件就是持續化的,意思就是這個事件會保持掛起狀態,即便回調函數被執行。若是你想讓它變爲非掛起狀態,能夠在回調函數中調用event_del()
。函數
任什麼時候候事件的回調函數觸發都會重置持續化事件中的超時狀態。所以,若是的事件有EV_READ/EV_PERSIST
而且設置了5秒超時,那麼有兩種狀況會觸發這個事件:oop
當socket能夠進行讀取的時候翻譯
當5s超時到期的時候code
<?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); }