msg_receive() - semaphore函数
msg_receive()
(PHP 4 >= 4.3.0, PHP 5, PHP 7)
Receive a message from a message queue
说明
msg_receive(resource $queue,int $desiredmsgtype,int &$msgtype,int $maxsize, mixed&$message[,bool $unserialize=TRUE
[,int $flags= 0[,int &$errorcode]]]): boolmsg_receive() will receive the first message from the specified$queueof the type specified by$desiredmsgtype.
参数
$queueMessage queue resource handle
$desiredmsgtype If$desiredmsgtypeis 0, the message from the front of the queue is returned. If$desiredmsgtypeis greater than 0, then the first message of that type is returned. If$desiredmsgtypeis less than 0, the first message on the queue with a type less than or equal to the absolute value of$desiredmsgtypewill be read. If no messages match the criteria, your script will wait until a suitable message arrives on the queue. You can prevent the script from blocking by specifying MSG_IPC_NOWAIT
in the$flagsparameter.
The type of the message that was received will be stored in this parameter.
$maxsizeThe maximum size of message to be accepted is specified by the$maxsize; if the message in the queue is larger than this size the function will fail(unless you set$flagsas described below).
$messageThe received message will be stored in$message, unless there were errors receiving the message.
$unserialize If set to TRUE
, the message is treated as though it was serialized using the same mechanism as the session module. The message will be unserialized and then returned to your script. This allows you to easily receive arrays or complex object structures from other PHP scripts, or if you are using the WDDX serializer, from any WDDX compatible source.
If$unserializeis FALSE
, the message will be returned as a binary-safe string.
The optional$flagsallows you to pass flags to the low-level msgrcv system call. It defaults to 0, but you may specify one or more of the following values(by adding or ORing them together).
MSG_IPC_NOWAIT | If there are no messages of the$desiredmsgtype, return immediately and do not wait. The function will fail and return an integer value corresponding to MSG_ENOMSG . |
MSG_EXCEPT | Using this flag in combination with a$desiredmsgtypegreater than 0 will cause the function to receive the first message that is not equal to$desiredmsgtype. |
MSG_NOERROR | If the message is longer than$maxsize, setting this flag will truncate the message to$maxsizeand will not signal an error. |
If the function fails, the optional$errorcodewill be set to the value of the system errno variable.
返回值
成功时返回TRUE
,或者在失败时返回FALSE
。
Upon successful completion the message queue data structure is updated as follows:msg_lrpidis set to the process-ID of the calling process,msg_qnumis decremented by 1 andmsg_rtimeis set to the current time.
参见
msg_remove_queue()
Destroy a message queuemsg_send()
Send a message to a message queuemsg_stat_queue()
Returns information from the message queue data structuremsg_set_queue()
Set information in the message queue data structure
It looks like msg_receive() allocates a memory with size $maxsize, and only then tries to receive a message from queue into allocated memory. Because my script dies with $maxsize = 1 Gib, but works with $maxsize = 10 Kib.
Consider this e.g. Linux situation: Now run: in terminal #1 php5 receive.php in terminal #2 php5 receive.php in terminal #3 php5 send.php Showing messages from queue will flip-flop. It means you run once send.php, the message will be shown in terminal #1. Second run it will be in t#2, third #1 and so on.
It seems that a maxsize of 2Mb is some sort of a threshold for php, above that msg_receive() starts to use a lot of CPU (with a sender that is pushing messages non-stop receiving 10000 messages jumps up from 0.01 sec to 1.5 sec on my computer) so try to stay below that thresholod if you can.
The behaviour of msg_recieve function depends on value of $desiredmsgtype: If zero: the first message with any $msgtype will be recieved. Positive: the first message with $msgtype = desiredmsgtype Negative: the first message with $msgtype
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!