《PHP實例:php事件驅(qū)動化設(shè)計詳解》要點:
本文介紹了PHP實例:php事件驅(qū)動化設(shè)計詳解,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP應(yīng)用本文實例講述了php事件驅(qū)動化設(shè)計.分享給大家供大家參考,具體如下:
PHP應(yīng)用最近在做一個需要用到異步php的項目, 翻閱php源碼的時候,發(fā)現(xiàn)了三個沒有用過的模塊,sysvsem,sysvshm,sysvmsg,一番研究以后,受益非淺.
PHP應(yīng)用在php中有這么一族函數(shù),他們是對unix的v ipc函數(shù)族的包裝.
它們很少被人們用到,但是它們卻很強大.巧妙的運用它們,可以讓你事倍功半.
PHP應(yīng)用它們包括:
PHP應(yīng)用信號量(semaphores)
共享內(nèi)存(shared memory)
進程間通信(inter-process messaging, ipc)
PHP應(yīng)用基于這些,我們完全有可能將php包裝成一基于消息驅(qū)動的系統(tǒng).
PHP應(yīng)用但是,首先,我們需要介紹幾個重要的基礎(chǔ):
PHP應(yīng)用1. ftok
PHP應(yīng)用int ftok ( string pathname, string proj )
PHP應(yīng)用ftok將一個路徑名pathname和一個項目名(必須為一個字符), 轉(zhuǎn)化成一個整形的用來使用系統(tǒng)v ipc的key
PHP應(yīng)用2. ticks
PHP應(yīng)用ticks是從php 4.0.3開始才加入到php中的,它是一個在 declare 代碼段中解釋器每執(zhí)行 n 條低級語句就會發(fā)生的事件.n 的值是在 declare 中的 directive 部分用 ticks=n 來指定的.
PHP應(yī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";
}
}
unregister_tick_function("getstatus");
PHP應(yīng)用這個就基本相當(dāng)于:
PHP應(yī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");
PHP應(yīng)用消息,我現(xiàn)在用一個例子來說明,如何結(jié)合ticks來實現(xiàn)php的消息通信.
PHP應(yīng)用
$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應(yīng)用在這個例子中,首先將我們的php執(zhí)行process加入到一個由ftok生成的key所獲得的消息隊列中.
PHP應(yīng)用然后,通過ticks,沒隔倆個語句,就去查詢一次消息隊列.
PHP應(yīng)用然后模擬了消息發(fā)送.
PHP應(yīng)用在瀏覽器訪問這個腳本,結(jié)果如下:
PHP應(yīng)用
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應(yīng)用看到這里是不是,大家已經(jīng)對怎么模擬php為事件驅(qū)動已經(jīng)有了一個概念了? 別急,我們繼續(xù)完善.
PHP應(yīng)用2. 信號量
PHP應(yīng)用信號量的概念,大家應(yīng)該都很熟悉.通過信號量,可以實現(xiàn)進程通信,競爭等. 再次就不贅述了,只是簡單的列出php中提供的信號量函數(shù)集
PHP應(yīng)用sem_acquire -- acquire a semaphore
sem_get -- get a semaphore id
sem_release -- release a semaphore
sem_remove -- remove a semaphore
PHP應(yīng)用具體信息,可以翻閱php手冊.
PHP應(yīng)用3. 內(nèi)存共享
PHP應(yīng)用php sysvshm提供了一個內(nèi)存共享方案:sysvshm,它是和sysvsem,sysvmsg一個系列的,但在此處,我并沒有使用它,我使用的shmop系列函數(shù),結(jié)合ticks
PHP應(yīng)用
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);
?>
PHP應(yīng)用運行這個例子,不斷刷新,我們可以看到index在遞增.
PHP應(yīng)用單單使用這個shmop就能完成一下,php腳本之間共享數(shù)據(jù)的功能:以及,比如緩存,計數(shù)等等.
PHP應(yīng)用更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《php socket用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
PHP應(yīng)用希望本文所述對大家PHP程序設(shè)計有所幫助.
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/2832.html