最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php事件驅(qū)動(dòng)化設(shè)計(jì)詳解

 更新時(shí)間:2016年11月10日 11:27:47   作者:flandycheng  
這篇文章主要介紹了php事件驅(qū)動(dòng)化設(shè)計(jì),結(jié)合實(shí)例形式較為詳細(xì)的分析了php事件驅(qū)動(dòng)化所涉及的信號(hào)量、共享內(nèi)存與進(jìn)程間通信相關(guān)概念與操作技巧,需要的朋友可以參考下

本文實(shí)例講述了php事件驅(qū)動(dòng)化設(shè)計(jì)。分享給大家供大家參考,具體如下:

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

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

它們包括:

信號(hào)量(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è)整形的用來(lái)使用系統(tǒng)v ipc的key

2. ticks

ticks是從php 4.0.3開(kāi)始才加入到php中的,它是一個(gè)在 declare 代碼段中解釋器每執(zhí)行 n 條低級(jí)語(yǔ)句就會(huì)發(fā)生的事件。n 的值是在 declare 中的 directive 部分用 ticks=n 來(lái)指定的。

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è)例子來(lái)說(shuō)明,如何結(jié)合ticks來(lái)實(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ì)列中。

然后,通過(guò)ticks,沒(méi)隔倆個(gè)語(yǔ)句,就去查詢(xún)一次消息隊(duì)列。

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

在瀏覽器訪(fǎng)問(wèn)這個(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)對(duì)怎么模擬php為事件驅(qū)動(dòng)已經(jīng)有了一個(gè)概念了? 別急,我們繼續(xù)完善。

2. 信號(hào)量

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

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

具體信息,可以翻閱php手冊(cè)。

3. 內(nèi)存共享

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

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php curl用法總結(jié)》、《php socket用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

津南区| 夹江县| 精河县| 万载县| 新竹市| 潼关县| 台江县| 蓬溪县| 济源市| 淮南市| 海丰县| 天津市| 柳江县| 平谷区| 阿坝| 长顺县| 治县。| 长宁区| 吉水县| 横峰县| 保康县| 桐梓县| 遂昌县| 遂溪县| 大姚县| 平凉市| 临高县| 海门市| 岫岩| 夏津县| 汉中市| 洪江市| 眉山市| 磐安县| 胶州市| 来凤县| 赞皇县| 隆回县| 六盘水市| 同心县| 古交市|