PHP Message Queue

Source: Internet
Author: User
PHP Message Queuing

php-two classes for Message Queuing and process communication through shared memory

To implement Message Queuing, you can use more specialized tools, such as Apache ActiveMQ, Memcacheq ....., here are two basic simple implementations:

Use the Memcache method to implement

 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->maxs Ize, $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->maxsiz e); 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 < $offset) {return null;} $front = $this->get (' front ')% $this->maxsize; $rear = $this->get (' rear ')% $this->maxsize; $keys [] = $th Is->getkeybypos (($front + $offset)% $this->maxsize) $num = 1;for ($pos = ($front + $offset + 1)% $this->maxsi Ze $pos! = $rear; $pos = ($pos + 1)% $this->maxsize) {$keys [] = $this->getkeybypos ($pos); $num ++;if ($limit > 0 && $li MIT = = $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;}}? >
Use shared memory queue implementation Click to view original address

?

Using PHP to work with Linux Message Queuing to complete interprocess communication

Interprocess communication is a crucial step when we develop a system that needs to run in a multi-process manner. Message Queuing is a way of communicating between processes in a Linux system.
On the concept and implementation of process communication in Linux system view: http://www.ibm.com/developerworks/cn/linux/l-ipc/
On the concept and implementation of Message Queuing in Linux system view: http://www.ibm.com/developerworks/cn/linux/l-ipc/part4/
PHP's Sysvmsg module is a package of System V Message Queuing function families in System V IPC supported by Linux systems. We need to use the functions provided by the SYSVMSG module to communicate between processes. Let's take a look at the sample code _1:

 
  
? The result of this code is as follows:
Resource (4) of type (sysvmsg queue) Array (    [Msg_perm.uid] = [    msg_perm.gid] = [    msg_ Perm.mode] = 438    [Msg_stime] + 0    [Msg_rtime] = 0    [Msg_ctime] = 1279849495    [Msg_qnum] =& Gt 0    [Msg_qbytes] = 16384    [Msg_lspid] = 0    [msg_lrpid] + 0) Array (    [msg_perm.uid] = 1000< C11/>[msg_perm.gid] [    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!
? You can see that "hello,world!" was successfully read from the message queue String

The main functions in the sample code are listed below:

ftok (string $pathname, string $proj) is explained in the manual: Convert a pathname and a project IdentiFi Er to a System V IPC key. The key value returned by this function uniquely corresponds to a message queue in the Linux system. This function needs to be called before a reference to a message queue can be obtained. Msg_get_queue (int $key [, int $perms]) Msg_get_queue () returns a reference to a message queue based on the key value passed in. If there is no message queue in the Linux system corresponding to the key value, Msg_get_queue () will create a new message queue. The second parameter of the function needs to pass in an int value as the permission value for the newly created message queue, which defaults to 0666. This permission value is the same as the value used in the Linux command chmod, because everything is a file in a Linux system.  Msg_send (Resource $queue, int $msgtype, mixed $message [, BOOL $serialize [, BOOL $blocking [, int & $errorcode]] As the name implies, this function is used to write data to the message queue. Msg_stat_queue (resource $queue) This function returns the metadata for the message queue. The information in the Message Queuing metadata is complete, including the number of messages to be read in the message queue, the process ID of the last read-write queue, and so on. The sample code calls this function in line 8th to return the number of messages in the queue that are to be read Msg_qnum the value is 0. Msg_receive (Resource $queue, int $desiredmsgtype, int & $msgtype, int $maxsize, mixed & $message [, BOOL $unse rialize [, int $flags [, int & $errorcode]]]) msg_receive is used to read the data in the message queue. Msg_remove_queue (Resource $queue) Msg_remove_queue is used to destroy a queue. 
? The sample code _1 just shows the application of the PHP operation Message Queuing function. The following code specifically describes the scenario for interprocess communication
 
  
The result of the operation is:
no.0 child process was created, the PID are 5249no.1 child process were created, the PID is 5250no.2 child process was creat Ed, the PID is 5251no.3 child process were created, the PID is 5252no.4 child process was created, the PID is 5253process.5  251 is writing nowthis are process.5251 ' s dataprocess.5253 is writing nowprocess.5252 are writing nowprocess.5250 is writing NowThis is process.5253 's datathis is process.5252 's datathis is process.5250 ' s dataprocess.5249 are writing nowthis is PR ocess.5249 ' s data

Redis
Http://www.neatstudio.com/show-976-1.shtml

?

PHP comes with three message queue related functions
Http://www.zhangguangda.com/?p=89?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.