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

资源魔 35 0
比来正在做一个需求用到异步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为事情驱动曾经有了一个概念了? 别急,咱们持续欠缺。

3. 旌旗灯号量

旌旗灯号量的概念,各人应该都很相熟。经过旌旗灯号量,能够完成过程通讯,竞争等。 再次就没有赘述了,只是简略的列出PHP中提供的旌旗灯号量函数集。

sem_acquire -- Acquire a semaphore
sem_get -- Get a semaphore id
sem_release -- Release a semaphore
sem_remove -- Remove a semaphore

详细信息,能够翻阅PHP手册。

4. 内存同享

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剧本之间同享数据的性能:和,比方缓存,计数等等。

更多PHP相干常识,请拜访PHP中文网!

以上就是对于PHP你可能没有晓得的-PHP的事情驱动化设计的具体内容,更多请存眷资源魔其它相干文章!

标签: php开发教程 php开发资料 php开发自学 事件驱动化 设计模式

抱歉,评论功能暂时关闭!