您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php的libev扩展_PHP教程

2024/5/30 22:37:21发布34次查看
ev is a pecl extension providing inteface to libev library – high performance full-featured event loop written in c.
about libev 
libev is an event loop: you register interest in certain events (such as a file descriptor being readable or a timeout occurring), and it will manage these event sources and provide your program with events.
那么什么是libev呢,从网上摘录一段:
libev 是高性能事件循环/事件模型的网络库,并且包含大量新特性。 
它是继lievent和event perl module之后的一套全新网络库。它追求的目标:速度更快,bug更少,特性更多,体积更小。 www.2cto.com
它和libevent很像,按照作者的介绍,可以作为libevent的替代者,能够提供更高的性能。并不需要复杂的配置。
看起来和之前提到的libevent大有渊源,但是这个扩展的作者显然比较活跃,一周内提交了3个版本。
代码示例
timer的使用
stop();
    // stop the watcher if further calls cause more than 10 iterations
    ev::iteration() >= 10 and $w->stop();
});
// 创建一个已停止的timer,手工start才有效
$w_stopped = evtimer::createstopped(10, 5, function($w) {
    echo callback of a timer created as stopped\n;
// stop the watcher after 2 iterations
    ev::iteration() >= 2 and $w->stop();
});
// loop until ev::stop() is called or all of watchers stop
ev::run();
// start and look if it works
$w_stopped->start();
echo run single iteration\n;
ev::run(ev::run_once);
echo restart the second watcher and try to handle the same events, but don't block\n;
$w2->again();
ev::run(ev::run_nowait);
$w = new evtimer(10, 0, function() {});
echo running a blocking loop\n;
ev::run();
echo end\n;
?>
输出内容
2 seconds elapsed
is called every second, is launched after 2 seconds
iteration = 1
is called every second, is launched after 2 seconds
iteration = 2
is called every second, is launched after 2 seconds
iteration = 3
is called every second, is launched after 2 seconds
iteration = 4
is called every second, is launched after 2 seconds
iteration = 5
run single iteration
callback of a timer created as stopped
restart the second watcher and try to handle the same events, but don't block
running a blocking loop
is called every second, is launched after 2 seconds
iteration = 8
is called every second, is launched after 2 seconds
iteration = 9
is called every second, is launched after 2 seconds
iteration = 10
end
i/o事件
例1
例2
stop();
    // stop write watcher
    $w->stop();
$in = head / http/1.1\r\n;
    $in .= host: google.co.uk\r\n;
    $in .= connection: close\r\n\r\n;
if (!socket_write($socket, $in, strlen($in))) {
        trigger_error(failed writing $in to socket, e_user_error);
    }
$read_watcher = new evio($socket, ev::read, function ($w, $re)
        use ($socket, $e_nonblocking) {
        // socket is readable. recv() 20 bytes using non-blocking mode
        $ret = socket_recv($socket, $out, 20, msg_dontwait);
if ($ret) {
            echo $out;
        } elseif ($ret === 0) {
            // all read
            $w->stop();
            socket_close($socket);
            return;
        }
// caught einprogress, eagain, or ewouldblock
        if (in_array(socket_last_error(), $e_nonblocking)) {
            return;
        }
$w->stop();
        socket_close($socket);
    });
ev::run();
});
$result = socket_connect($socket, $address, $service_port);
ev::run();
?>
输出
http/1.1 301 moved permanently
location: http://www.google.co.uk/
content-type: text/html; charset=utf-8
date: sun, 23 dec 2012 16:08:27 gmt
expires: tue, 22 jan 2013 16:08:27 gmt
cache-control: public, max-age=2592000
server: gws
content-length: 221
x-xss-protection: 1; mode=block
x-frame-options: sameorigin
connection: close
http://www.bkjia.com/phpjc/477830.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/477830.htmltecharticleev is a pecl extension providing inteface to libev library high performance full-featured event loop written in c. about libev libev is an event loop: you register interest in certa...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product