位置:- 正文

php消息队列的介绍(php 队列)

编辑:rootadmin

推荐整理分享php消息队列的介绍(php 队列),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:php消息队列kafka,php消息队列面试题及答案,php消息队列kafka,php消息队列的理解,php消息队列rabbitmq,php消息队列的介质是什么,php消息队列的介质是什么,php消息队列的理解,内容如对您有帮助,希望把文章链接给更多的朋友!

php消息队列的介绍(php 队列)

515

说明

1、消息队列是存放在内存中的一个队列。

2、由于消息队列去数据是,只有一个进程能去到,所以不需要额外的锁或信号量。

实例

echo"parentprogresspid:{$parentPid\n";$childList=array();//创建消息队列,以及定义消息类型(类似于数据库中的库)$id=ftok(__FILE__,'m');$msgQueue=msg_get_queue($id);constMSG_TYPE=1;//生产者functionproducer(){global$msgQueue;$pid=posix_getpid();$repeatNum=5;for($i=1;$i<=$repeatNum;$i++){$str="({$pid)progresscreate!{$i";msg_send($msgQueue,MSG_TYPE,$str);$rand=rand(1,3);sleep($rand);//消费者functionconsumer(){global$msgQueue;$pid=posix_getpid();$repeatNum=6;for($i=1;$i<=$repeatNum;$i++){$rel=msg_receive($msgQueue,MSG_TYPE,$msgType,1024,$message);echo"{$message|consumer({$pid)destroy\n";$rand=rand(1,3);sleep($rand);functioncreateProgress($callback){$pid=pcntl_fork();if($pid==-1){//创建失败exit("forkprogresserror!\n");elseif($pid==0){//子进程执行程序$pid=posix_getpid();$callback();exit("({$pid)childprogressend!\n");else{//父进程执行程序return$pid;//3个写进程for($i=0;$i<3;$i++){$pid=createProgress('producer');$childList[$pid]=1;echo"createproducerchildprogress:{$pid\n";//2个写进程for($i=0;$i<2;$i++){$pid=createProgress('consumer');$childList[$pid]=1;echo"createconsumerchildprogress:{$pid\n";//等待所有子进程结束while(!empty($childList)){$childPid=pcntl_wait($status);if($childPid>0){unset($childList[$childPid]);echo"({$parentPid)mainprogressend!\n";
本文链接地址:https://www.jiuchutong.com/zhishi/310745.html 转载请保留说明!
下一篇链接:https://www.jiuchutong.com/zhishi/310746.html
免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络