Php message queue php-implement message queue and process communication through shared memory. you can use professional tools such as ApacheActiveMQ and memcacheq ....., The following are two basic implementation methods: Use the memcache method to implement & lt ;? Php/* php Message Queue
Php-two types of message queue and process communication through shared memory
To implement message queues, you can use professional tools, such as Apache ActiveMQ, memcacheq ....., The following are two basic implementation methods:
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-> 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 ('F Ront ') % $ 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 [] = $ 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 ;}}?>
Use the shared memory queue to click to view the original address
?
Use PHP to operate a Linux message queue for inter-process communication
When the system we develop needs to run in multi-process mode, inter-process communication becomes a crucial link. Message queue is a way of communication between processes in Linux.
The concept and implementation of process communication in Linux can be viewed: http://www.ibm.com/developerworks/cn/linux/l-ipc/
The concept and implementation of message queue in Linux system can be viewed: http://www.ibm.com/developerworks/cn/linux/l-ipc/part4/
The sysvmsg module of PHP encapsulates the System V message queue function family in the System v ipc supported by Linux. We need to use the functions provided by the sysvmsg module for inter-process communication. Let's take a look at the example code _ 1:
? The running result of this code is as follows:
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!
? We can see that "Hello, World!" has been successfully read from the message queue !" String
The following lists the main functions in the sample code:
The ftok (string $ pathname, string $ proj) manual explains Convert a pathname and a project identifier to a System v ipc key. The key value returned by this function corresponds to a message queue in linux. You must call this function before obtaining the reference of a message queue. Msg_get_queue (int $ key [, int $ perms]) msg_get_queue () returns a reference to a message queue based on the input key value. If no message queue corresponds to a key value in linux, msg_get_queue () creates a new message queue. The second parameter of the function requires an int value as the permission value for the newly created Message Queue. the default value is 0666. This permission value is the same as the value used in the linux command chmod, because everything in the linux system is a file. Msg_send (resource $ queue, int $ msgtype, mixed $ message [, bool $ serialize [, bool $ blocking [, int & $ errorcode]) as the name suggests, this function is used to write data to a message queue. The msg_stat_queue (resource $ queue) function returns the metadata of the message queue. The information in the message queue metadata is complete, including the number of messages to be read in the message queue and the process ID of the last read/write queue. The sample code returns the number of messages to be read in the queue msg_qnum value in the array returned by calling the function in row 8th. Msg_receive (resource $ queue, int $ desiredmsgtype, int & $ msgtype, int $ maxsize, mixed & $ message [, bool $ unserialize [, int $ flags [, int & $ errorcode]) msg_receive is used to read data in the message queue. Msg_remove_queue (resource $ queue) msg_remove_queue is used to destroy a queue.
? The example code _ 1 only shows the application of PHP to operate message queue functions. The following code describes the scenario of inter-process communication.
The running result is:
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
?
Three message queue-related functions provided by php
Http://www.zhangguangda.com /? P = 89?