msg_receive

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

msg_receive — メッセージキューからメッセージを受信する

説明

function msg_receive(
    SysvMessageQueue $queue,
    int $desired_message_type,
    int &$received_message_type,
    int $max_message_size,
    mixed &$message,
    bool $unserialize = true,
    int $flags = 0,
    int &$error_code = null
): bool

msg_receive() は、指定した queue から指定した desired_message_type の最初のメッセージを受信します。

パラメータ

queue

メッセージキュー

desired_message_type

desired_message_type が 0 の場合、 キューの先頭にあるメッセージが返されます。desiredmsgtype が 0 より大きな値の場合、その型のメッセージのうち一番最初にあるものが返されます。 desired_message_type が 0 より小さな値の場合、 desired_message_type の絶対値と同じかそれより小さい型のメッセージのうち一番最初にあるものが返されます。 条件を満たすメッセージがない場合は、 条件を満たすメッセージがキューに投入されるまで待ち続けます。 パラメータ flags に MSG_IPC_NOWAIT を指定することで、 ブロックモードではなくすることが可能です。

received_message_type

受信したメッセージの型がこのパラメータに保存されます。

max_message_size

読み込むメッセージの最大サイズは mmax_message_size で指定します。もしキューにあるメッセージのサイズがこれより大きい場合、 (以下で説明する flags が設定されていない限り) この関数は失敗します。

message

エラーが発生しなければ、 受信したメッセージは message に保存されます。

unserialize

このパラメータが true に設定されている場合、 メッセージはセッションモジュールと同様の方法でシリアライズされているものとみなされます。 メッセージは元の状態に復元されたうえでスクリプトに返されます。 これにより、配列やオブジェクト構造体のような複雑な形式のデータを他の PHP スクリプトから簡単に受信することが可能となります。 また、もし WDDX シリアライザを使用しているなら、あらゆる WDDX 互換のソースからデータを受け取ることが可能となります。

unserialize が false の場合、 メッセージはバイナリセーフな文字列として返されます。

flags

オプションの flags により、低レベルの msgrcv システムコールにフラグを渡すことが可能です。デフォルト値は 0 ですが、以下の値のうちのいくつかを(値を足すかあるいは論理和ととることで) 指定することが可能です。

msg_receive のフラグの値
MSG_IPC_NOWAIT desired_message_type を満たすメッセージが存在しない場合に、 待ち続けずにすぐに結果を返します。 関数は失敗し、MSG_ENOMSG に対応する整数値を返します。
MSG_EXCEPT このフラグを正の desired_message_type と組み合わせて使用すると、この関数は desired_message_type 以外の型をもつ 最初のメッセージを受信するようになります。
MSG_NOERROR このフラグを設定しておくと、メッセージが max_message_size より大きい場合に そのメッセージを max_message_size までに切り詰め、 エラーを返しません。
error_code

エラーが発生した場合は、オプションの error_code にシステムの errno 値が設定されます。

戻り値

成功した場合に true を、失敗した場合に false を返します。

処理が正常に完了すると、メッセージキューデータ構造体は以下のように更新されます。 msg_lrpid には呼び出し元のプロセス ID が 設定され、msg_qnum が 1 減少し、 msg_rtime が現在の時刻に設定されます。

変更履歴

バージョン 説明
8.0.0 引数 queue は、 SysvMessageQueue のインスタンスを期待するようになりました。 これより前のバージョンでは、リソースが期待されていました。

参考

  • msg_remove_queue() - メッセージキューを破棄する
  • msg_send() - メッセージキューにメッセージを送信する
  • msg_stat_queue() - メッセージキューデータ構造体の情報を返す
  • msg_set_queue() - メッセージキューデータ構造体の情報を設定する
+add a note

User Contributed Notes 5 notes

up
2
marbledore at mail dot ru
15 years ago
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.
up
1
eimers at mehrkanal dot com
18 years ago
<?php error_reporting(E_ALL);
/**
 * Example for sending and receiving Messages via the System V Message Queue
 *
 * To try this script run it synchron/asynchron twice times. One time with ?typ=send and one time with ?typ=receive
 *
 * @author          Thomas Eimers - Mehrkanal GmbH
 *
 * This document is distributed in the hope that it will be useful, but without any warranty;
 * without even the implied warranty of merchantability or fitness for a particular purpose.
 */

header('Content-Type: text/plain; charset=ISO-8859-1');
echo "Start...\n";

// Create System V Message Queue. Integer value is the number of the Queue
$queue = msg_get_queue(100379);

// Sendoptions
$message='nachricht';     // Transfering Data
$serialize_needed=false;  // Must the transfer data be serialized ?
$block_send=false;        // Block if Message could not be send (Queue full...) (true/false)
$msgtype_send=1;          // Any Integer above 0. It signeds every Message. So you could handle multible message
                          // type in one Queue.

// Receiveoptions
$msgtype_receive=1;       // Whiche type of Message we want to receive ? (Here, the type is the same as the type we send,
                          // but if you set this to 0 you receive the next Message in the Queue with any type.
$maxsize=100;             // How long is the maximal data you like to receive.
$option_receive=MSG_IPC_NOWAIT; // If there are no messages of the wanted type in the Queue continue without wating.
                          // If is set to NULL wait for a Message.

// Send or receive 20 Messages
for ($i=0;$i<20;$i++) {
  sleep(1);
  // This one sends
  if ($_GET['typ']=='send') {
    if(msg_send($queue,$msgtype_send, $message,$serialize_needed, $block_send,$err)===true) {
      echo "Message sendet.\n";
    } else {
      var_dump($err);
    }
  // This one received
  } else {
    $queue_status=msg_stat_queue($queue);
    echo 'Messages in the queue: '.$queue_status['msg_qnum']."\n";

    // WARNUNG: nur weil vor einer Zeile Code noch Nachrichten in der Queue waren, muss das jetzt nciht mehr der Fall sein!
    if ($queue_status['msg_qnum']>0) {
      if (msg_receive($queue,$msgtype_receive ,$msgtype_erhalten,$maxsize,$daten,$serialize_needed, $option_receive, $err)===true) {
              echo "Received data".$daten."\n";
      } else {
              var_dump($err);
      }
    }
  }
}

?>
up
1
soger
7 years ago
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.
up
0
marvel at post dot cz
18 years ago
Consider this e.g. Linux situation:

<?php
//file send.php
$ip = msg_get_queue(12340);
msg_send($ip,8,"abcd",false,false,$err);
//-----------------------------------------------------
<?php
//file receive.php
$ip = msg_get_queue(12340);

msg_receive($ip,0,$msgtype,4,$data,false,null,$err);
echo "msgtype {$msgtype} data {$data}\n";

msg_receive($ip,0,$msgtype,4,$data,false,null,$err);
echo "msgtype {$msgtype} data {$data}\n";
?>

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.
up
0
webmaster at toolshed51 dot com
23 years ago
This is meant to be run as your apache user in a terminal, call script in note of msg_send and they will communicate.

#! /usr/bin/env php
<?php
    $MSGKEY = 519051; // Message

    $msg_id = msg_get_queue ($MSGKEY, 0600);

    while (1) {
        if (msg_receive ($msg_id, 1, $msg_type, 16384, $msg, true, 0, $msg_error)) {
            if ($msg == 'Quit') break;
            echo "$msg\n";
        } else {
            echo "Received $msg_error fetching message\n";
            break;
        }
    }

    msg_remove_queue ($msg_id);
?>