博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 事件驱动 消息机制 共享内存
阅读量:4553 次
发布时间:2019-06-08

本文共 3984 字,大约阅读时间需要 13 分钟。

最近在做一个需要用到异步PHP的项目, 翻阅PHP源码的时候,发现了三个没有用过的模块,sysvsem,sysvshm,sysvmsg,一番研究以后,受益非浅。在PHP中有这么一族函数,他们是对UNIX的V IPC函数族的包装。它们很少被人们用到,但是它们却很强大。巧妙的运用它们,可以让你事倍功半。它们包括:信号量(Semaphores)共享内存(Shared Memory)进程间通信(Inter-Process Messaging, IPC)基于这些,我们完全有可能将PHP包装成一基于消息驱动的系统。但是,首先,我们需要介绍几个重要的基础:1. ftokint ftok ( string pathname, string proj )//ftok将一个路径名pathname和一个项目名(必须为一个字符), 转化成一个整形的用来使用系统V IPC的key2. ticksTicks是从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为事件驱动已经有了一个概念了? 别急,我们继续完善。2. 信号量信号量的概念,大家应该都很熟悉。通过信号量,可以实现进程通信,竞争等。 再次就不赘述了,只是简单的列出PHP中提供的信号量函数集。sem_acquire -- Acquire a semaphoresem_get -- Get a semaphore idsem_release -- Release a semaphoresem_remove -- Remove a semaphore具体信息,可以翻阅PHP手册。3. 内存共享PHP sysvshm提供了一个内存共享方案:sysvshm,它是和sysvsem,sysvmsg一个系列的,但在此处,我并没有使用它,我使用的shmop系列函数,结合TIcksfunction memoryUsage(){ printf("%s: %s
", 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
", 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脚本之间共享数据的功能:以及,比如缓存,计数等等。

 

转载于:https://www.cnblogs.com/lost-1987/articles/3084074.html

你可能感兴趣的文章
Cognos中新建SQLserver数据源的步骤
查看>>
HttpClient连接超时及读取超时
查看>>
SQL优化方法
查看>>
SEO必须掌握的高级搜索指令
查看>>
生产者消费者模型
查看>>
ORACLE 字符串超长问题解决方案
查看>>
使用ZooKeeper协调多台Web Server的定时任务处理(方案1)
查看>>
20171116 每周例行报告
查看>>
[C#] SHA1校验函数用法
查看>>
linux 下 VMware 提示Unable to change virtual machine power state:
查看>>
洛谷P1585 魔法阵
查看>>
线程 题待做
查看>>
PL/SQL可以连oracle,但是jdbc连不上 【转】
查看>>
使用 highlight.js 在网页中高亮显示java 代码 【原】
查看>>
[转]高颜值、好用、易扩展的微信小程序 UI 库,Powered by 有赞
查看>>
[转]SQL Server如何启用xp_cmdshell组件
查看>>
[转]微擎应用笔记3--manifest.xml文件使用说明
查看>>
Codeforces 1000C Covered Points Count 【前缀和优化】
查看>>
python高效读取文件、文件改写
查看>>
gulp
查看>>