PHP之高性能I/O框架:Libevent

Posted 飞鸿影的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP之高性能I/O框架:Libevent相关的知识,希望对你有一定的参考价值。

Swoole

Swoole里也提供了一些直接操作底层 epoll/kqueue事件循环的接口,可将其他扩展创建的 socketphp代码中 stream/socket扩展创建的 socket等加入到Swoole的 EventLoop中。

文档:https://wiki.swoole.com/wiki/page/242.html

这里我也简单介绍一下。

基本使用

swoole_tcp_server.php

 
   
   
 
  1. <?php

  2. /**

  3. * Created by PhpStorm.

  4. * Date: 2018/6/30

  5. */

  6. use Swoole\Event;

  7. $socket = stream_socket_server ("tcp://0.0.0.0:9201", $errno, $errstr);

  8. if (false === $socket ) {

  9.    echo "$errstr($errno)\n";

  10.    exit();

  11. }

  12. if (!$socket) die($errstr."--".$errno);

  13. // stream_set_blocking($socket,0); //可以去掉,没有涉及到read这个socket

  14. echo "waiting client...\n";

  15. //accept事件回调函数,参数分别是$fd

  16. function ev_accept($socket){

  17.    global $master;

  18.    $connection = stream_socket_accept($socket);

  19.    //参数的设置将会影响到像 fgets() 和 fread() 这样的函数从资源流里读取数据。

  20.    //在非阻塞模式下,调用 fgets() 总是会立即返回;而在阻塞模式下,将会一直等到从资源流里面获取到数据才能返回。

  21.    stream_set_blocking($connection, 0);//如果不设置,后续读取会阻塞

  22.    $id = (int)$connection;

  23.    echo "new Client $id\n";

  24.    Event::add($connection, 'ev_read', null, SWOOLE_EVENT_READ);

  25. }

  26. //read事件回调函数,参数是fd

  27. function ev_read($buffer)

  28. {

  29.    $receive = '';

  30.    //循环读取并解析客户端消息

  31.    while( 1 ) {

  32.        $read = @fread($buffer, 2);

  33.        //客户端异常断开

  34.        if($read === '' || $read === false){

  35.            break;

  36.        }

  37.        $pos = strpos($read, "\n");

  38.        if($pos === false)

  39.        {

  40.            $receive .= $read;

  41.            // echo "received:".$read.";not all package,continue recdiveing\n";

  42.        }else{

  43.            $receive .= trim(substr ($read,0,$pos+1));

  44.            $read = substr($read,$pos+1);

  45.            switch ( $receive ){

  46.                case "quit":

  47.                    echo "client close conn\n";

  48.                    //关闭客户端连接

  49.                    Event::del($buffer);//删除事件

  50.                    fclose($buffer);//断开客户端连接

  51.                    break;

  52.                default:

  53.                    // echo "all package:\n";

  54.                    echo $receive."\n";

  55.                    break;

  56.            }

  57.            $receive='';

  58.        }

  59.    }

  60. }

  61. Event::add($socket, 'ev_accept', null, SWOOLE_EVENT_READ);

  62. echo  "start run...\n";

  63. //进入事件循环

  64. Event::wait(); //PHP5.4或更高版本不需要加此函数

  65. //下面这句不会被执行

  66. echo "This code will not be executed.\n";

Event::add()等方法也有对应的函数,例如 swoole_event_add()

定时器

swoole提供了两个定时器:

  • swoole_timer_tick 周期定时器,面向对象写法: Timer::tick

  • swoole_timer_after 一次性定时器,面向对象写法: Timer::after

swoole定时器的精度是毫秒。

示例:

 
   
   
 
  1. <?php

  2. /**

  3. * Created by PhpStorm.

  4. * Date: 2018/6/30

  5. */

  6. use Swoole\Timer;

  7. Timer::after(1000, function(){

  8.    echo time(). " hello\n";

  9.    Timer::tick(1000, function($timer_id, $params){

  10.        static $c = 0;

  11.        echo time(). " hello $params $c\n";

  12.        $c++;

  13.        if($c > 5){

  14.            Timer::clear($timer_id);

  15.        }

  16.    }, 'this is param');

  17. });

说明: Timer::after回调函数没有参数, Timer::tick回调函数参数是定时器ID及附加参数。

总结

我曾经查阅了 Workerman的源码,看到作者把常见的Event(系统自带Select、libevet、Event、Ev、Swoole)进行了一次封装,对外暴露了统一的接口(add、del、loop),大家有兴趣可以看看。

参考

1、php libevent扩展的简单用例 - NickBai - 博客园
https://www.cnblogs.com/nickbai/articles/6197689.html
2、PHP使用pcntl和libevent 实现Timer功能 | 博客水木
http://www.4u4v.net/php-uses-pcntl-and-libevent-achieve-timer-function.html
3、socket服务的模型以及实现(4)–单进程IO复用libevent
http://www.xtgxiso.com/socket%e6%9c%8d%e5%8a%a1%e7%9a%84%e6%a8%a1%e5%9e%8b%e4%bb%a5%e5%8f%8a%e5%ae%9e%e7%8e%b04-%e5%8d%95%e8%bf%9b%e7%a8%8bio%e5%a4%8d%e7%94%a8libevent/
4、libevent中的bufferevent原理 - nengm - 博客园
https://www.cnblogs.com/nengm1988/p/8203784.html
5、EventLoop-Swoole-Swoole文档中心
https://wiki.swoole.com/wiki/page/242.html


以上是关于PHP之高性能I/O框架:Libevent的主要内容,如果未能解决你的问题,请参考以下文章

高性能I/O框架库Libevent

php libevent扩展

PHP 7 编译安装开启 libevent 扩展支持

PHP7 安装 event 扩展

PHP libevent扩展安装

php libevent扩展的简单用例