關於PHP你可能不知道的-PHP的事件驅動化設計

· 作者: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
      
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", $now), 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腳本之間共享數據的功能:以及,比如緩存,計數等等。

    未完待續
發佈了31 篇原創文章 · 獲贊 9 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章