Zihao

Make small but daily progress

0%

PHP多线程系列之libevent

libevent是一个基于事件驱动的高性能网络库。支持多种 I/O 多路复用技术, epollpolldev/pollselectkqueue 等;支持 I/O,定时器和信号等事件;注册事件优先级。

PHP libevent扩展安装

libevent扩展依赖于原始的libevent库,必须先把libevent库安装。

(1)安装libevent库

1
2
3
4
5
6
wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz
tar zxvf libevent–2.0.20–stable.tar.gz
cd libevent–2.0.20–stable/
./configure —prefix=/usr/local/libevent–2.0.20/
make
make install

(2)安装libevent扩展

1
2
3
4
5
6
wget http://pecl.php.net/get/libevent-0.1.0.tgz
tar –zxvf libevent–0.1.0.tgz
cd libevent–0.1.0
./configure —with–php–config=/usr/local/php54/bin/php–config —with–libevent=/usr/local/libevent–2.0.20/
make && make install
#php.ini添加extension=libevent.so

PHP Libevent扩展介绍:

(1)常量

libevent1.png

(2)函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
event_base_free() 释放资源,这不能销毁绑定事件
event_base_loop() 处理事件,根据指定的base来处理事件循环
event_base_loopbreak() 立即取消事件循环,行为各break语句相同
event_base_loopexit() 在指定的时间后退出循环
event_base_new() 创建并且初始事件
event_base_priority_init() 设定事件的优先级
event_base_set() 关联事件到事件base
event_buffer_base_set() 关联缓存的事件到event_base
event_buffer_disable() 禁用一个缓存的事件
event_buffer_enable() 启用一个指定的缓存的事件
event_buffer_fd_set() 改变一个缓存的文件系统描述
event_buffer_free() 释放缓存事件
event_buffer_new() 建立一个新的缓存事件
event_buffer_priority_set() 缓存事件的优先级设定
event_buffer_read() 读取缓存事件中的数据
event_buffer_set_callback() 给缓存的事件设置或重置回调hansh函数
event_buffer_timeout_set() 给一个缓存的事件设定超时的读写时间
event_buffer_watermark_set 设置读写事件的水印标记
event_buffer_write() 向缓存事件中写入数据
event_add() 向指定的设置中添加一个执行事件
event_del() 从设置的事件中移除事件
event_free() 清空事件句柄
event_new() 创建一个新的事件
event_set() 准备想要在event_add中添加事件
PHP Libevent扩展使用:

案例

例1:5s后触发callback

1
2
3
4
5
6
7
8
9
10
$base = event_base_new();
$event = event_new();

event_set($event, 0, EV_TIMEOUT, function() {
echofunction called;
});
event_base_set($event, $base);

event_add($event, 5000000);
event_base_loop($base);

例2:打印输入流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function print_line($fd, $events, $arg)
{
static $max_requests = 0;
$max_requests++;
if ($max_requests == 10) {
// exit loop after 10 writes
event_base_loopexit($arg[1]);
}
echo fgets($fd);

?
// create base and event
$base = event_base_new();
$event = event_new();

$fd = STDIN;
// set event flags
event_set($event, $fd, EV_READ | EV_PERSIST, ‘print_line’, array($event, $base));
// set event base
event_base_set($event, $base);
// enable event
event_add($event);
// start event loop
event_base_loop($base);

例3:实现简单web server

在cli执行后,打开浏览器2000端口试试看。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
$socket = stream_socket_server (‘tcp://0.0.0.0:2000’, $errno, $errstr);
stream_set_blocking($socket, 0);

$base = event_base_new();
$event = event_new();
event_set($event, $socket, EV_READ | EV_PERSIST, ‘ev_accept’, $base);
event_base_set($event, $base);
event_add($event);
event_base_loop($base);

function ev_accept($socket, $flag, $base)
{
$connection = stream_socket_accept($socket);
stream_set_blocking($connection, 0);

$buffer = event_buffer_new($connection, ‘ev_read’, NULL, ‘ev_error’, $connection);
event_buffer_base_set($buffer, $base);
event_buffer_timeout_set($buffer, 30, 30);
event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
event_buffer_priority_set($buffer, 10);
event_buffer_enable($buffer, EV_READ | EV_PERSIST);

$GLOBALS[‘_’] = $buffer; //这个buffer一定要赋给个全局的变量 貌似是传值过程中的bug 或者5.3.8的闭包还是有问题?
}

function ev_error($buffer, $error, $connection)
{
event_buffer_disable($buffer, EV_READ | EV_WRITE);
event_buffer_free($buffer);
fclose($connection);
}

function ev_read($buffer, $connection)
{
while ($read = event_buffer_read($buffer, 256)) {

}
fwrite($connection , date(‘Y-m-d H:i:s’));
ev_error($buffer , ” , $connection);
}
  • 本文作者: Zihao Yao
  • 本文链接: https://yaozihao.com/php_thread_libeven/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

欢迎关注我的其它发布渠道