Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

5.15. libevent

5.15.1. event_base_loop

永遠循環

		
<?php
$timeouts = 10000000;
//callback function
function func($fd, $event,$arg) {
$time = time();
for($i=0;$i<2;$i++) {
    echo "Timer-$arg: $time : out-$i\n";
    sleep(3);
}
}
//create base and event 
$base = event_base_new();
for($i=0;$i<2;$i++) {
$event[$i] = event_new();
//set event flags
event_set($event[$i], $i , EV_PERSIST, 'func', "$i");
//set event base
event_base_set($event[$i], $base);
//enable event
event_add($event[$i], $timeouts);
}
//start event loop
event_base_loop($base);
		
		

運行一次然後退出

event_base_loop($base, EVLOOP_ONCE );
		
		
<?php
$timeouts = 10;
//callback function
function func($fd, $event,$arg) {
$time = time();
    echo "Timer-$arg: $time\n";
}
//create base and event 
$base = event_base_new();
for($i=0;$i<10;$i++) {
$event[$i] = event_new();
//set event flags
event_set($event[$i], $i , EV_PERSIST, 'func', "$i");
//set event base
event_base_set($event[$i], $base);
//enable event
event_add($event[$i], $timeouts);
}
//start event loop
event_base_loop($base, EVLOOP_ONCE );
event_base_loop($base, EVLOOP_ONCE );
event_base_loop($base, EVLOOP_ONCE );
event_base_loop($base, EVLOOP_ONCE );		
		
		

5.15.2.