• 技术文章 >后端开发 >php教程

    php事件驱动化设计

    不言不言2018-04-09 17:39:35原创1149
    这篇文章主要介绍了php事件驱动化设计,现在分享给大家,有需要的朋友可以参考下

    本文实例讲述了php事件驱动化设计。分享给大家供大家参考,具体如下:

    最近在做一个需要用到异步php的项目, 翻阅php源码的时候,发现了三个没有用过的模块,sysvsem,sysvshm,sysvmsg,一番研究以后,受益非浅。

    在php中有这么一族函数,他们是对unix的v ipc函数族的包装。
    它们很少被人们用到,但是它们却很强大。巧妙的运用它们,可以让你事倍功半。

    它们包括:

    信号量(semaphores)
    共享内存(shared memory)
    进程间通信(inter-process messaging, ipc)

    基于这些,我们完全有可能将php包装成一基于消息驱动的系统。

    但是,首先,我们需要介绍几个重要的基础:

    1. ftok

    int ftok ( string pathname, string proj )

    ftok将一个路径名pathname和一个项目名(必须为一个字符), 转化成一个整形的用来使用系统v ipc的key

    2. ticks

    ticks是从php 4.0.3开始才加入到php中的,它是一个在 declare 代码段中解释器每执行 n 条低级语句就会发生的事件。n 的值是在 declare 中的 directive 部分用 ticks=n 来指定的。

    function getstatus($arg){
      print_r(connection_status());
      debug_print_backtrace();
    }
    reigster_tick_function("getstatus", true);
    declare(ticks=1){
      for($i =1; $i<999; $i++){
     echo "hello";
     }
    }
    unregister_tick_function("getstatus");

    这个就基本相当于:

    function getstatus($arg){
      print_r(connection_status());
      debug_print_backtrace();
    }
    reigster_tick_function("getstatus", true);
    declare(ticks=1){
      for($i =1; $i<999; $i++){
     echo "hello"; getstatus(true);
     }
    }
    unregister_tick_function("getstatus");

    消息,我现在用一个例子来说明,如何结合ticks来实现php的消息通信。

    $mesg_key = ftok(__file__, 'm');
    $mesg_id = msg_get_queue($mesg_key, 0666);
    function fetchmessage($mesg_id){
     if(!is_resource($mesg_id)){
      print_r("mesg queue is not ready");
     }
     if(msg_receive($mesg_id, 0, $mesg_type, 1024, $mesg, false, msg_ipc_nowait)){
      print_r("process got a new incoming msg: $mesg ");
     }
    }
    register_tick_function("fetchmessage", $mesg_id);
    declare(ticks=2){
     $i = 0;
     while(++$i < 100){
      if($i%5 == 0){
    msg_send($mesg_id, 1, "hi: now index is :". $i);
      }
     }
    }
    //msg_remove_queue($mesg_id);

    在这个例子中,首先将我们的php执行process加入到一个由ftok生成的key所获得的消息队列中。

    然后,通过ticks,没隔俩个语句,就去查询一次消息队列。

    然后模拟了消息发送。

    在浏览器访问这个脚本,结果如下:

    process got a new incoming msg: s:19:"hi: now index is :5";
    process got a new incoming msg: s:20:"hi: now index is :10";
    process got a new incoming msg: s:20:"hi: now index is :15";
    process got a new incoming msg: s:20:"hi: now index is :20";
    process got a new incoming msg: s:20:"hi: now index is :25";
    process got a new incoming msg: s:20:"hi: now index is :30";
    process got a new incoming msg: s:20:"hi: now index is :35";
    process got a new incoming msg: s:20:"hi: now index is :40";
    process got a new incoming msg: s:20:"hi: now index is :45";
    process got a new incoming msg: s:20:"hi: now index is :50";
    process got a new incoming msg: s:20:"hi: now index is :55";
    process got a new incoming msg: s:20:"hi: now index is :60";
    process got a new incoming msg: s:20:"hi: now index is :65";
    process got a new incoming msg: s:20:"hi: now index is :70";
    process got a new incoming msg: s:20:"hi: now index is :75";
    process got a new incoming msg: s:20:"hi: now index is :80";
    process got a new incoming msg: s:20:"hi: now index is :85";
    process got a new incoming msg: s:20:"hi: now index is :90";
    process got a new incoming msg: s:20:"hi: now index is :95";

    看到这里是不是,大家已经对怎么模拟php为事件驱动已经有了一个概念了? 别急,我们继续完善。

    2. 信号量

    信号量的概念,大家应该都很熟悉。通过信号量,可以实现进程通信,竞争等。 再次就不赘述了,只是简单的列出php中提供的信号量函数集

    sem_acquire -- acquire a semaphore
    sem_get -- get a semaphore id
    sem_release -- release a semaphore
    sem_remove -- remove a semaphore

    具体信息,可以翻阅php手册。

    3. 内存共享

    php sysvshm提供了一个内存共享方案:sysvshm,它是和sysvsem,sysvmsg一个系列的,但在此处,我并没有使用它,我使用的shmop系列函数,结合ticks

    function memoryusage(){
     printf("%s: %s<br/>", date("h:i:s",time()), memory_get_usage());
     //var_dump(debug_backtrace());
     //var_dump(__function__);
     //debug_print_backtrace();
    }
    register_tick_function("memoryusage");
    declare(ticks=1){
    $shm_key = ftok(__file__, 's');
    $shm_id = shmop_open($shm_key, 'c', 0644, 100);
    }
    printf("size of shared memory is: %s<br/>", shmop_size($shm_id));
    $shm_text = shmop_read($shm_id, 0, 100);
    eval($shm_text);
    if(!empty($share_array)){
     var_dump($share_array);
     $share_array['id'] += 1;
    }else{
     $share_array = array('id' => 1);
    }
    $out_put_str = "$share_array = " . var_export($share_array, true) .";";
    $out_put_str = str_pad($out_put_str, 100, " ", str_pad_right);
    shmop_write($shm_id, $out_put_str, 0);
    ?>

    运行这个例子,不断刷新,我们可以看到index在递增。

    单单使用这个shmop就能完成一下,php脚本之间共享数据的功能:以及,比如缓存,计数等等。

    相关推荐:

    PHP事件机制的实现

    详细解析:关于PHP事件驱动问题_PHP教程

    php入门到就业线上直播课:查看学习

    以上就是php事件驱动化设计的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:php 设计 驱动
    上一篇:PHP中设置时区方法 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 工具包分享:PHP实现滑块验证图片• 请教QQ互联的代码是如何写的? • 没有上载的分数了,vip次数也用完了啊 • PHP登录跳转,该怎么处理 • 注册信息添加到数据库,该怎么处理
    1/1

    PHP中文网