>  기사  >  백엔드 개발  >  PHP中运用pcntl和libevent实现Timer功能

PHP中运用pcntl和libevent实现Timer功能

WBOY
WBOY원래의
2016-06-13 11:17:55880검색

PHP中使用pcntl和libevent实现Timer功能


<?phpfunction newChild($func_name) {    echo "enter newChild\n";    $args = func_get_args();    unset($args[0]);    $pid =  pcntl_fork();    if ($pid == 0) {        function_exists($func_name) and exit(call_user_func_array($func_name, $args)) or exit(-1);    } else if($pid == -1) {        echo "Couldn't create child process";    } else {        return $pid;    }}function on_timer() {    echo "timer called\n";}/** * @param $func string, function name * @param $timeouts int, microtimes for time delay */function timer($func, $timeouts){    echo "enter timer\n";    $base = event_base_new();    $event = event_new();    event_set($event, 0, EV_TIMEOUT, $func);    event_base_set($event, $base);    event_add($event, $timeouts);    event_base_loop($base);}$pid = newChild("timer", "on_timer", 5000000);if ($pid > 0) {    echo "master process exit\n";}




성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.