+ -
当前位置:首页 → 问答吧 → 关于PHP你可能不知道的-PHP的事件驱动化设计

关于PHP你可能不知道的-PHP的事件驱动化设计

时间:2008-08-18

来源:互联网

怎么贴过来,格式就乱了。。。。。


· 作者:laruence(http://www.laruence.com/)
· 本文地址: http://www.laruence.com/2008/04/21/101.html
· 转载请注明出处                                            

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


在PHP中有这么一族函数,他们是对UNIX的V IPC函数族的包装。


它们很少被人们用到,但是它们却很强大。巧妙的运用它们,可以让你事倍功半。


它们包括:


信号量(Semaphores)
共享内存(Shared Memory)
进程间通信(Inter-Process Messaging, IPC)
基于这些,我们完全有可能将PHP包装成一基于消息驱动的系统。


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


1. ftok


  1. int ftok ( string pathname, string proj )
  2. //ftok将一个路径名pathname和一个项目名(必须为一个字符), 转化成一个整形的用来使用系统V IPC的key
复制代码

2. ticks


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


  1. function getStatus($arg){
  2. print_r connection_status();
  3.   debug_print_backtrace();
  4. }
  5. reigster_tick_function("getStatus", true);


  6. declare(ticks=1){
  7.   for($i =1; $i<999; $i++){
  8.   echo "hello";
  9.   }

  10. }

  11. unregister_tick_function("getStatus");
复制代码

这个就基本相当于:

  1. function getStatus($arg){

  2. print_r connection_status();
  3. debug_print_backtrace();


  4. }

  5. reigster_tick_function("getStatus", true);

  6. declare(ticks=1){


  7. for($i =1; $i<999; $i++){

  8. echo "hello"; getStatus(true);


  9. }
  10. }
  11. unregister_tick_function("getStatus");
复制代码

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

  1. $mesg_key = ftok(__FILE__, 'm');
  2. $mesg_id = msg_get_queue($mesg_key, 0666);

  3. function fetchMessage($mesg_id){

  4. if(!is_resource($mesg_id)){


  5. print_r("Mesg Queue is not Ready");
  6. }
  7. if(msg_receive($mesg_id, 0, $mesg_type, 1024, $mesg, false, MSG_IPC_NOWAIT)){
  8. print_r("Process got a new incoming MSG: $mesg ");
  9. }
  10. }
  11. register_tick_function("fetchMessage", $mesg_id);
  12. declare(ticks=2){
  13. $i = 0;
  14. while(++$i < 100){
  15. if($i%5 == 0){
  16. msg_send($mesg_id, 1, "Hi: Now Index is :". $i);
  17. }
  18. }
  19. }
  20. //msg_remove_queue($mesg_id);
复制代码

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


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


然后模拟了消息发送。


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

  1. Process got a new incoming MSG: s:19:"Hi: Now Index is :5";
  2. Process got a new incoming MSG: s:20:"Hi: Now Index is :10";
  3. Process got a new incoming MSG: s:20:"Hi: Now Index is :15";
  4. Process got a new incoming MSG: s:20:"Hi: Now Index is :20";
  5. Process got a new incoming MSG: s:20:"Hi: Now Index is :25";
  6. Process got a new incoming MSG: s:20:"Hi: Now Index is :30";
  7. Process got a new incoming MSG: s:20:"Hi: Now Index is :35";
  8. Process got a new incoming MSG: s:20:"Hi: Now Index is :40";
  9. Process got a new incoming MSG: s:20:"Hi: Now Index is :45";
  10. Process got a new incoming MSG: s:20:"Hi: Now Index is :50";
  11. Process got a new incoming MSG: s:20:"Hi: Now Index is :55";
  12. Process got a new incoming MSG: s:20:"Hi: Now Index is :60";
  13. Process got a new incoming MSG: s:20:"Hi: Now Index is :65";
  14. Process got a new incoming MSG: s:20:"Hi: Now Index is :70";
  15. Process got a new incoming MSG: s:20:"Hi: Now Index is :75";
  16. Process got a new incoming MSG: s:20:"Hi: Now Index is :80";
  17. Process got a new incoming MSG: s:20:"Hi: Now Index is :85";
  18. Process got a new incoming MSG: s:20:"Hi: Now Index is :90";
  19. Process got a new incoming MSG: s:20:"Hi: Now Index is :95";
复制代码

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


2. 信号量


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

  1. sem_acquire -- Acquire a semaphore
  2. sem_get -- Get a semaphore id
  3. sem_release -- Release a semaphore
  4. sem_remove -- Remove a semaphore
复制代码

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


3. 内存共享


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

  1. <?php

  2. function memoryUsage(){         printf("%s: %s<br/>", date("H:i:s", $now), memory_get_usage());

  3.         //var_dump(debug_backtrace());

  4.         //var_dump(__FUNCTION__);

  5.         //debug_print_backtrace();


  6. }


  7. register_tick_function("memoryUsage");



  8. declare(ticks=1){


  9.                 $shm_key = ftok(__FILE__, 's');

  10.         $shm_id = shmop_open($shm_key, 'c', 0644, 100);


  11. }


  12. printf("Size of Shared Memory is: %s<br/>", shmop_size($shm_id));


  13. $shm_text = shmop_read($shm_id, 0, 100);


  14. eval($shm_text);


  15. if(!empty($share_array)){

  16.         var_dump($share_array);

  17.         $share_array['id'] += 1;


  18. }else{

  19.         $share_array = array('id' => 1);


  20. }
  21. $out_put_str = "$share_array = " . var_export($share_array, true) .";";


  22. $out_put_str = str_pad($out_put_str, 100, " ", STR_PAD_RIGHT);


  23. shmop_write($shm_id, $out_put_str, 0);


  24. ?>
复制代码



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




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


未完待续

作者: laruence   发布时间: 2008-08-18

:smile: 看看

作者: nianjin   发布时间: 2008-08-18

不错顶之

作者: bobohu88   发布时间: 2010-06-13

鸟哥以后有什么帖子需要编辑和转的,直接站内pm我,我来做编辑就可以

作者: 7u5   发布时间: 2010-06-20

shmop这个东西很危险,不小心老是报内存出错
BTW:shmop在WIN下是废的

作者: 追梦ren   发布时间: 2010-06-20

{:5_246:}

作者: xia1005   发布时间: 2010-06-29

PPC的高亮不是一般的垃圾... 还没有我博客的好看..

作者: TankMe   发布时间: 2010-06-29

PPC的高亮不是一般的垃圾... 还没有我博客的好看..
TankMe 发表于 2010-6-29 18:02



    哥,其实好多人的都没你的blog漂亮

作者: panjinww   发布时间: 2010-06-29

{:5_232:}

作者: mmxcq   发布时间: 2010-07-02

哈哈,牛,,厉害.

作者: sghsnmh   发布时间: 2010-07-09

高级

作者: fly_sharp   发布时间: 2010-07-13

学习...

作者: orgthing   发布时间: 2010-11-10

鸟哥必出精品

作者: lanye   发布时间: 2010-11-14

先顶后看

作者: goldfinger   发布时间: 2010-12-28

此人NBA,
一出必属精品,
顶一个,

作者: phptristan   发布时间: 2011-01-08

这是个高级东西

作者: marshuoyi   发布时间: 2011-06-19

锁定的帖子还能回复?

作者: xiaochong0302   发布时间: 2013-04-17