PHP Amqp通道回调循环
PHP Amqp channel callback loop
The example code for RabbitMQ 状态
Our code will block while our $channel has callbacks. Whenever we
receive a message our $callback function will be passed the received
message.
使用这段代码
while(count($channel->callbacks)) {
$channel->wait();
}
这让我很困惑,因为 the default timeout for PhpAmqpLib\Channel\AbstractChannel::wait
is forever.
public function wait($allowed_methods = null, $non_blocking = false, $timeout = 0)
所以如果 wait
永远阻塞,代码将如何到达 while
循环的第二次迭代?
是否可以安全地说 while
循环只有在 wait
被传递给 $timeout > 0
时才需要?
您显示的代码将代表 AMQP 消息的工作者或处理者
因为它将侦听来自 AMQP 服务器的丢弃消息,所以这些消息将永远 waiting/listening/looping。
wait
调用的超时参数是在放弃之前等待下一条消息 的时间。默认值,如你所说,是"forever",意思是"until a message arrives".
但是,一旦收到并处理了一条消息,wait
调用就会退出;它也许可以命名为 waitForNextEvent()
。您可以在链接到的来源中看到:
if ($this->should_dispatch_method($allowed_methods, $method_sig)) {
return $this->dispatch($method_sig, $args, $amqpMessage);
}
所以要接收多条消息,您需要多次调用 wait()
。通常,在消费者中,您想无限次调用它,因此您可以只使用 while(true)
,但如果您取消注册所有回调,则允许循环退出,这为您提供了一种优雅退出的方式。
The example code for RabbitMQ 状态
Our code will block while our $channel has callbacks. Whenever we receive a message our $callback function will be passed the received message.
使用这段代码
while(count($channel->callbacks)) {
$channel->wait();
}
这让我很困惑,因为 the default timeout for PhpAmqpLib\Channel\AbstractChannel::wait
is forever.
public function wait($allowed_methods = null, $non_blocking = false, $timeout = 0)
所以如果 wait
永远阻塞,代码将如何到达 while
循环的第二次迭代?
是否可以安全地说 while
循环只有在 wait
被传递给 $timeout > 0
时才需要?
您显示的代码将代表 AMQP 消息的工作者或处理者
因为它将侦听来自 AMQP 服务器的丢弃消息,所以这些消息将永远 waiting/listening/looping。
wait
调用的超时参数是在放弃之前等待下一条消息 的时间。默认值,如你所说,是"forever",意思是"until a message arrives".
但是,一旦收到并处理了一条消息,wait
调用就会退出;它也许可以命名为 waitForNextEvent()
。您可以在链接到的来源中看到:
if ($this->should_dispatch_method($allowed_methods, $method_sig)) {
return $this->dispatch($method_sig, $args, $amqpMessage);
}
所以要接收多条消息,您需要多次调用 wait()
。通常,在消费者中,您想无限次调用它,因此您可以只使用 while(true)
,但如果您取消注册所有回调,则允许循环退出,这为您提供了一种优雅退出的方式。