百科狗-知识改变命运!
--

msg_receive() - semaphore函数

梵高12个月前 (11-21)阅读数 12#技术干货
文章标签返回值

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]]]): bool

msg_receive() will receive the first message from the specified$queueof the type specified by$desiredmsgtype.

参数

$queue

Message 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.

$msgtype

The type of the message that was received will be stored in this parameter.

$maxsize

The 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).

$message

The 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.

$flags

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).

Flag values for msg_receive
MSG_IPC_NOWAITIf 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_EXCEPTUsing 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.
$errorcode

If the function fails, the optional$errorcodewill be set to the value of the system errno variable.

返回值

成功时返回TRUE,或者在失败时返回FALSE

msg_receive() - semaphore函数

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 queue
  • msg_send()Send a message to a message queue
  • msg_stat_queue()Returns information from the message queue data structure
  • msg_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

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)