Libevent 是一个用C语言编写的、轻量级的开源高性能I/O框架,支持多种 I/O 多路复用技术: epoll、 poll、 dev/poll、 select 和 kqueue 等;支持 I/O,定时器和信号等事件;注册事件优先级。PHP提供了对应的扩展 libevent、 Event 。

相比 libevent ext, event ext 拥有更好的性能表现和更全面的API,可以让我们更友好的使用 Libevent。

安装 Libevent 库

php event 扩展 依赖 Libevent 库,需要先编译安装(注意是 Libevent 库,非 libevent 扩展)。

  1. git clone --depth=1 https://github.com/nmathewson/Libevent.git
  2. cd Libevent
  3. ./autogen.sh
  4. ./configure --prefix=/usr/local/libevent
  5. make && make install

安装 Event 扩展

可以直接使用 pecl 安装

  1. pecl install event
  2. downloading event-2.5.3.tgz ...
  3. Starting to download event-2.5.3.tgz (140,913 bytes)
  4. ..............................done: 140,913 bytes
  5. 50 source files, building
  6. running: phpize
  7. Configuring for:
  8. PHP Api Version: 20180731
  9. Zend Module Api No: 20180731
  10. Zend Extension Api No: 320180731
  11. Enable internal debugging in Event [no] :
  12. Enable sockets support in Event [yes] :
  13. libevent installation prefix [/usr] : /usr/local/libevent
  14. ...
  15. Build process completed successfully
  16. Installing '/usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/event.so'
  17. install ok: channel://pecl.php.net/event-2.5.3
  18. Extension event enabled in php.ini

查看安装结果

  1. [root@localhost ~]# php --ri event
  2. event
  3. Event support => enabled
  4. Sockets support => enabled
  5. Debug support => disabled
  6. Extra functionality support including HTTP, DNS, and RPC => enabled
  7. OpenSSL support => enabled
  8. Thread safety support => disabled
  9. Extension version => 2.5.3
  10. libevent2 headers version => 2.2.0-alpha-dev