php中事件驅(qū)動(dòng)化設(shè)計(jì)的示例分析-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“php中事件驅(qū)動(dòng)化設(shè)計(jì)的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“php中事件驅(qū)動(dòng)化設(shè)計(jì)的示例分析”這篇文章吧。

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供沙灣網(wǎng)站建設(shè)、沙灣做網(wǎng)站、沙灣網(wǎng)站設(shè)計(jì)、沙灣網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、沙灣企業(yè)網(wǎng)站模板建站服務(wù),10余年沙灣做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

具體如下:

最近在做一個(gè)需要用到異步php的項(xiàng)目, 翻閱php源碼的時(shí)候,發(fā)現(xiàn)了三個(gè)沒有用過的模塊,sysvsem,sysvshm,sysvmsg,一番研究以后,受益非淺。

在php中有這么一族函數(shù),他們是對unix的v ipc函數(shù)族的包裝。
它們很少被人們用到,但是它們卻很強(qiáng)大。巧妙的運(yùn)用它們,可以讓你事倍功半。

它們包括:

信號量(semaphores)
共享內(nèi)存(shared memory)
進(jìn)程間通信(inter-process messaging, ipc)

基于這些,我們完全有可能將php包裝成一基于消息驅(qū)動(dòng)的系統(tǒng)。

但是,首先,我們需要介紹幾個(gè)重要的基礎(chǔ):

1. ftok

int ftok ( string pathname, string proj )

ftok將一個(gè)路徑名pathname和一個(gè)項(xiàng)目名(必須為一個(gè)字符), 轉(zhuǎn)化成一個(gè)整形的用來使用系統(tǒng)v ipc的key

2. ticks

ticks是從php 4.0.3開始才加入到php中的,它是一個(gè)在 declare 代碼段中解釋器每執(zhí)行 n 條低級語句就會(huì)發(fā)生的事件。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");

這個(gè)就基本相當(dāng)于:

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");

消息,我現(xiàn)在用一個(gè)例子來說明,如何結(jié)合ticks來實(shí)現(xiàn)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);

在這個(gè)例子中,首先將我們的php執(zhí)行process加入到一個(gè)由ftok生成的key所獲得的消息隊(duì)列中。

然后,通過ticks,沒隔倆個(gè)語句,就去查詢一次消息隊(duì)列。

然后模擬了消息發(fā)送。

在瀏覽器訪問這個(gè)腳本,結(jié)果如下:

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";

看到這里是不是,大家已經(jīng)對怎么模擬php為事件驅(qū)動(dòng)已經(jīng)有了一個(gè)概念了? 別急,我們繼續(xù)完善。

2. 信號量

信號量的概念,大家應(yīng)該都很熟悉。通過信號量,可以實(shí)現(xiàn)進(jìn)程通信,競爭等。 再次就不贅述了,只是簡單的列出php中提供的信號量函數(shù)集

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

具體信息,可以翻閱php手冊。

3. 內(nèi)存共享

php sysvshm提供了一個(gè)內(nèi)存共享方案:sysvshm,它是和sysvsem,sysvmsg一個(gè)系列的,但在此處,我并沒有使用它,我使用的shmop系列函數(shù),結(jié)合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);
?>

運(yùn)行這個(gè)例子,不斷刷新,我們可以看到index在遞增。

單單使用這個(gè)shmop就能完成一下,php腳本之間共享數(shù)據(jù)的功能:以及,比如緩存,計(jì)數(shù)等等。

以上是“php中事件驅(qū)動(dòng)化設(shè)計(jì)的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

標(biāo)題名稱:php中事件驅(qū)動(dòng)化設(shè)計(jì)的示例分析-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://muchs.cn/article10/dsppdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站導(dǎo)航、定制開發(fā)、營銷型網(wǎng)站建設(shè)面包屑導(dǎo)航、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)