Based on SystemVMessagequeue PHP message queue encapsulation original article, reproduced please indicate the source: http://www.huyanping.cn /? P = 235
Author: Jenner
System V Message queue is a process communication (IPC) method that facilitates the implementation of the producer-consumer model. one or more producers write messages to the queue, multiple producers can then obtain messages from the queue for processing.
Project address: https://github.com/huyanping/Zebra-PHP-Framework
This Wrapper supports:
- Process Communication
- Set the maximum queue capacity (in bytes)
- Get the number of current queues
- Modifying attributes of a queue
Note: to modify the maximum queue capacity, make sure that your script runs under root.
Msg_type = $ msg_type; $ this-> serialize_needed = $ serialize_needed; $ this-> block_send = $ block_send; $ this-> option_receive = $ option_receive; $ this-> maxsize = $ maxsize; $ this-> init_queue ($ ipc_filename, $ msg_type );} /*** initialize a queue * @ param $ ipc_filename * @ param $ msg_type * @ throws Exception */public function init_queue ($ ipc_filename, $ msg_type) {$ this-> key_t = $ this-> get_ipc_key ($ ipc_filename, $ ms G_type); $ this-> queue = msg_get_queue ($ this-> key_t); if (! $ This-> queue) throw new Exception ('MSG _ get_queue failed ');} /*** @ param $ ipc_filename * @ param $ msg_type * @ return int * @ throws Exception */public function get_ipc_key ($ ipc_filename, $ msg_type) {$ key_t = ftok ($ ipc_filename, $ msg_type); if ($ key_t = 0) throw new Exception ('ftok error'); return $ key_t ;} /*** get A * @ return bool * @ throws Exception */public function get () {$ queue_status = $ This-> status (); if ($ queue_status ['MSG _ qnum']> 0) {if (msg_receive ($ this-> queue, $ this-> msg_type, $ msgtype_erhalten, $ this-> maxsize, $ data, $ this-> serialize_needed, $ this-> option_receive, $ err) === true) {return $ data ;} else {throw new Exception ($ err) ;}} else {return false ;}} /*** write queue ** @ param $ message * @ throws Exception */public function put ($ message) {if (! Msg_send ($ this-> queue, $ this-> msg_type, $ message, $ this-> serialize_needed, $ this-> block_send, $ err) === true) {throw new Exception ($ err);} return true;}/** returned array subscript: * msg_perm.uid The uid of the owner of the queue. user ID * msg_perm.gid The gid of the owner of the queue. user group ID * msg_perm.mode The file access mode of the queue. access Mode * msg_stime The time that the last message was sent to the que Ue. last queue write time * msg_rtime The time that the last message was received ed from the queue. last queue receipt time * msg_ctime The time that the queue was last changed. last modification time * msg_qnum The number of messages waiting to be read from the queue. number of queues awaiting reading * msg_qbytes The maximum number of bytes allowed in one message queue. maximum total size of messages allowed to be received in a message queue * On Linux, this value may be read and modified via/proc/sys/kern El/msgmnb. * msg_lspid The pid of the process that sent the last message to the queue. the process ID * msg_lrpid the pid of the process that sent the last message from The queue. the final process ID ** @ return array */public function status () {$ queue_status = msg_stat_queue ($ this-> queue); return $ queue_status ;} /*** get the current accumulation status of the queue * @ return mixed */public function size () {$ status = $ this-> status (); r Eturn $ status ['MSG _ qnum'];}/*** allows you to change the values of the msg_perm.uid, * msg_perm.gid, msg_perm.mode and msg_qbytes fields of the underlying message queue data structure * can be used to modify the maximum read data received by a queue running ** @ param $ key status subscript * @ param $ value status value *@ return bool */public function set_status ($ key, $ value) {$ this-> check_set_privilege ($ key); if ($ key = 'MSG _ qbytes ') return $ this-> set_max_queue _ Size ($ value); $ queue_status [$ key] = $ value; return msg_set_queue ($ this-> queue, $ queue_status );} /*** delete a queue * @ return bool */public function queue_remove () {return msg_remove_queue ($ this-> queue );} // modify the maximum number of bytes that a queue can accommodate. root permission is required/*** @ param $ size * @ return bool * @ throws Exception */public function set_max_queue_size ($ size) {$ user = get_current_user (); if ($ user! = 'Root') throw new Exception ('changing msg_qbytes needs root privileges '); return $ this-> set_status ('MSG _ qbytes', $ size );} /*** determine whether a queue exists * @ param $ key * @ return bool */public function queue_exists ($ key) {return msg_queue_exists ($ key );} /*** check the permission for modifying the queue status * @ param $ key * @ throws Exception */private function check_set_privilege ($ key) {$ privilege_field = array ('MSG _ perm. uid', 'MSG _ perm. gid ', 'MSG _ perm. mode'); if (! In_array ($ key, $ privilege_field) {throw new Exception ('You can only change msg_perm.uid, expiration, msg_perm.mode and msg_qbytes. And msg_qbytes needs root privileges ');}}}
The call method is as follows:
Write queue:
Put (mt_rand (0, 1000); echo $ messageQueue-> size (). PHP_EOL; sleep (1) ;}} catch (Exception $ e) {echo $ e-> getMessage ();}
Get queue:
Get (); sleep (1) ;}} catch (Exception $ e) {echo $ e-> getMessage ();}Delete queue:
Queue_remove ();} catch (Exception $ e) {echo $ e-> getMessage ();}