php讯息队列
程序员文章站
2022-05-24 19:50:35
...
php消息队列
php-通过共享内存实现消息队列和进程通信的两个类
实现消息队列,可以使用比较专业的工具,例如:Apache ActiveMQ、memcacheq…..,下面是两个基本简单的实现方式:
使用memcache方法来实现
memcache = Yl_Memcache::singleton (); $this->name = $name; $this->prefix = $prefix; $this->maxSize = $max_size; $this->add ( 'front', 0 ); $this->add ( 'rear', 0 ); $this->add ( 'size', 0 ); } function isEmpty() { return $this->get ( 'size' ) == 0; } function isFull() { return $this->get ( 'size' ) >= $this->maxSize; } function enQueue($data) { if ($this->isFull ()) { throw new Exception ( "Queue is Full" ); } $size = $this->increment ( 'size' ); $rear = $this->increment ( 'rear' ); $this->set ( ($rear - 1) % $this->maxSize, $data ); return $this; } function deQueue() { if ($this->isEmpty ()) { throw new Exception ( "Queue is Empty" ); } $this->decrement ( 'size' ); $front = $this->increment ( 'front' ); $this->delete ( ($front - 1) % $this->maxSize ); return $this; } function getTop() { return $this->get ( $this->get ( 'front' ) % $this->maxSize ); } function getAll() { return $this->getPage (); } function getPage($offset = 0, $limit = 0) { $size = $this->get ( 'size' ); if (0 == $size || $size get ( 'front' ) % $this->maxSize; $rear = $this->get ( 'rear' ) % $this->maxSize; $keys [] = $this->getKeyByPos ( ($front + $offset) % $this->maxSize ); $num = 1; for($pos = ($front + $offset + 1) % $this->maxSize; $pos != $rear; $pos = ($pos + 1) % $this->maxSize) { $keys [] = $this->getKeyByPos ( $pos ); $num ++; if ($limit > 0 && $limit == $num) { break; } } return array_values ( $this->memcache->get ( $keys ) ); } function makeEmpty() { $keys = $this->getAllKeys (); foreach ( $keys as $value ) { $this->delete ( $value ); } $this->delete ( "rear" ); $this->delete ( "front" ); $this->delete ( 'size' ); $this->delete ( "maxSize" ); } private function getAllKeys() { if ($this->isEmpty ()) { return array (); } $keys [] = $this->get ( 'front' ); for($pos = ($this->get ( 'front' ) % $this->maxSize + 1) % $this->maxSize; $pos != $this->get ( 'rear' ) % $this->maxSize; $pos = ($pos + 1) % $this->maxSize) { $keys [] = $pos; } return $keys; } private function add($pos, $data) { $this->memcache->add ( $this->getKeyByPos ( $pos ), $data ); return $this; } private function increment($pos) { return $this->memcache->increment ( $this->getKeyByPos ( $pos ) ); } private function decrement($pos) { $this->memcache->decrement ( $this->getKeyByPos ( $pos ) ); } private function set($pos, $data) { $this->memcache->save ( $data, $this->getKeyByPos ( $pos ) ); return $this; } private function get($pos) { return $this->memcache->get ( $this->getKeyByPos ( $pos ) ); } private function delete($pos) { return $this->memcache->delete ( $this->getKeyByPos ( $pos ) ); } private function getKeyByPos($pos) { return $this->prefix . $this->name . $pos; }}?>使用共享内存队列实现 点击查看原文地址
?
利用PHP操作Linux消息队列完成进程间通信
当我们开发的系统需要使用多进程方式运行时,进程间通信便成了至关重要的环节。消息队列(message queue)是Linux系统进程间通信的一种方式。
关于Linux系统进程通信的概念及实现可查看:http://www.ibm.com/developerworks/cn/linux/l-ipc/
关于Linux系统消息队列的概念及实现可查看:http://www.ibm.com/developerworks/cn/linux/l-ipc/part4/
PHP的sysvmsg模块是对Linux系统支持的System V IPC中的System V消息队列函数族的封装。我们需要利用sysvmsg模块提供的函数来进进程间通信。先来看一段示例代码_1:
resource(4) of type (sysvmsg queue)Array( [msg_perm.uid] => 1000 [msg_perm.gid] => 1000 [msg_perm.mode] => 438 [msg_stime] => 0 [msg_rtime] => 0 [msg_ctime] => 1279849495 [msg_qnum] => 0 [msg_qbytes] => 16384 [msg_lspid] => 0 [msg_lrpid] => 0)Array( [msg_perm.uid] => 1000 [msg_perm.gid] => 1000 [msg_perm.mode] => 438 [msg_stime] => 1279849495 [msg_rtime] => 0 [msg_ctime] => 1279849495 [msg_qnum] => 1 [msg_qbytes] => 16384 [msg_lspid] => 2184 [msg_lrpid] => 0)Hello,World!?可以看到已成功从消息队列中读取“Hello,World!”字符串
下面列举一下示例代码中的主要函数:
ftok ( string $pathname , string $proj ) 手册上给出的解释是:Convert a pathname and a project identifier to a System V IPC key。这个函数返回的键值唯一对应linux系统中一个消息队列。在获得消息队列的引用之前都需要调用这个函数。msg_get_queue ( int $key [, int $perms ] ) msg_get_queue()会根据传入的键值返回一个消息队列的引用。如果linux系统中没有消息队列与键值对应,msg_get_queue()将会创建一个新的消息队列。函数的第二个参数需要传入一个int值,作为新创建的消息队列的权限值,默认为0666。这个权限值与linux命令chmod中使用的数值是同一个意思,因为在linux系统中一切皆是文件。msg_send ( resource $queue , int $msgtype , mixed $message [, bool $serialize [, bool $blocking [, int &$errorcode ]]] ) 顾名思义,该函数用来向消息队列中写数据。msg_stat_queue ( resource $queue ) 这个函数会返回消息队列的元数据。消息队列元数据中的信息很完整,包括了消息队列中待读取的消息数、最后读写队列的进程ID等。示例代码在第8行调用该函数返回的数组中队列中待读取的消息数msg_qnum值为0。msg_receive ( resource $queue , int $desiredmsgtype , int &$msgtype , int $maxsize , mixed &$message [, bool $unserialize [, int $flags [, int &$errorcode ]]] ) msg_receive用于读取消息队列中的数据。msg_remove_queue ( resource $queue ) msg_remove_queue用于销毁一个队列。?示例代码_1只是展示了PHP操作消息队列函数的应用。下面的代码具体描述了进程间通信的场景运行结果为:
No.0 child process was created, the pid is 5249No.1 child process was created, the pid is 5250No.2 child process was created, the pid is 5251No.3 child process was created, the pid is 5252No.4 child process was created, the pid is 5253process.5251 is writing nowthis is process.5251's dataprocess.5253 is writing nowprocess.5252 is writing nowprocess.5250 is writing nowthis is process.5253's datathis is process.5252's datathis is process.5250's dataprocess.5249 is writing nowthis is process.5249's data
redis
http://www.neatstudio.com/show-976-1.shtml
?
php自带的三个消息队列相关的函数
http://www.zhangguangda.com/?p=89?
相关文章
相关视频
专题推荐
-
独孤九贱-php全栈开发教程
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
-
玉女心经-web前端开发教程
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
-
天龙八部-实战开发教程
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论