这篇文章主要介绍了php事件驱动化设计,现在分享给大家,有需要的朋友可以参考下
本文实例讲述了php事件驱动化设计。分享给大家供大家参考,具体如下:
最近在做一个需要用到异步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<p></p><p>这个就基本相当于:</p><p class="jb51code"></p><pre class="brush:php;toolbar:false">function getstatus($arg){
print_r(connection_status());
debug_print_backtrace();
}
reigster_tick_function("getstatus", true);
declare(ticks=1){
for($i =1; $i<p></p><p>消息,我现在用一个例子来说明,如何结合ticks来实现php的消息通信。<br></p><p class="jb51code"></p><pre class="brush:php;toolbar:false">$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 <p></p><p>在这个例子中,首先将我们的php执行process加入到一个由ftok生成的key所获得的消息队列中。</p><p>然后,通过ticks,没隔俩个语句,就去查询一次消息队列。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill5233" title="btpanel phpsite 宝塔面板PHP网站"><img
src="https://img.php.cn/upload/skill/000/000/081/179040786932301.jpg" alt="btpanel phpsite 宝塔面板PHP网站" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill5233" title="btpanel phpsite 宝塔面板PHP网站" class="overflowclass">btpanel phpsite 宝塔面板PHP网站</a>
<p class="overflowclass">宝塔面板 PHP 网站管理:站点创建、删除、启停、PHP 版本切换、域名管理、SSL证书管理、伪静态管理、数据库管理</p>
</div>
<a rel="nofollow" href="/xiazai/skill5233" title="btpanel phpsite 宝塔面板PHP网站" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><p>然后模拟了消息发送。</p><p>在浏览器访问这个脚本,结果如下:</p><p class="jb51code"></p><pre class="brush:php;toolbar:false">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",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);
?>
运行这个例子,不断刷新,我们可以看到index在递增。
单单使用这个shmop就能完成一下,php脚本之间共享数据的功能:以及,比如缓存,计数等等。
相关推荐:
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










