3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP の ZeroMQ で受信や送信のタイムアウト

Posted at

ZMQPoll でいわゆる select みたいなことができる。

例えば、送信の場合は次のようにする。

$pool = new ZMQPoll();

// ソケットを追加
$pool->add($socket, ZMQ::POLL_OUT);

// 結果を受け取る配列
$readable = [];
$writable = [];

// タイムアウト時間を 5000 ミリ秒に指定
$res = $pool->poll($readable, $writable, 5000);

// 戻り値は受信/送信が可能なソケットの総数
if ($res == 0) {
    throw new \RuntimeException("ZeroMQ: send timeout");
}

// 送信
$socket->send($data, ZMQ::MODE_DONTWAIT);

受信の場合は次のようになる。

$pool = new ZMQPoll();

// ソケットを追加
$pool->add($socket, ZMQ::POLL_IN);

// 結果を受け取る配列
$readable = [];
$writable = [];

// タイムアウト時間を 5000 ミリ秒に指定
$res = $pool->poll($readable, $writable, 5000);

// 戻り値は受信/送信が可能なソケットの総数
if ($res == 0) {
    throw new \RuntimeException("ZeroMQ: recv timeout");
}

// 受信
$data = $socket->recv(ZMQ::MODE_DONTWAIT);

$readable$writable には socket_select のように ZMQSocket が入っているので、複数のソケットで pool したときには、どのソケットが受信/送信が可能かを判断するために使用することができる。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?