在rabbitmq官方文档中提到消息发送到rabbitmq中有消息确认机制,且里面提到的是pika模块实现的。那么celery呢?celery已经用py-amqb模块实现了消息确认机制!
可以参考stackoverflow该网友自问自答的问题:
https://stackoverflow.com/questions/35996065/what-is-the-correct-way-to-confirm-a-publish-in-celery
question
I’m looking at tuning my celery/rabbitmq installation, and I found this article:
http://www.lshift.net/blog/2015/04/30/making-celery-play-nice-with-rabbitmq-and-bigwig/
It mentions doing the setting BROKER_TRANSPORT_OPTIONS = {‘confirm_publish’: True} if you want to guarantee message delivery (which I do). I’m having trouble finding any documentation on this setting for either rabbitmq or celery.
What is the correct way to confirm a publish in celery with rabbitmq? Where is the documentation for said feature?
answer
So to answer my own question: There is currently no documentation.
That said, I dug the source code for most of a morning and found the answer here:
py-amqp is the library that celery ultimately uses (via kombu–two packages deep here folks!). py-amqp reads that setting and has a wait when the confirm_publish option is set. This effectively makes a queuing process synchronous.
最后,我在粘贴下提到的代码:
def basic_publish_confirm(self, *args, **kwargs):
if not self._confirm_selected:
self._confirm_selected = True
self.confirm_select()
ret = self._basic_publish(*args, **kwargs)
self.wait(spec.Basic.Ack)
return ret
之前一直在找celery的publish消息的确认机制都没有头绪,没想到老外居然看源码就找出来了,看来我得好好学习深究源码了,最后,英语是硬伤!
我一直以为是默认设置就可以了,但是怎么验证呢?可以通过rabbitmq的后台web系统查看,如果在配置中添加BROKER_TRANSPORT_OPTIONS = {‘confirm_publish’: True},就会发现,在web系统的overview中Message rates中很明显的看到publisher confirm这个线条跑了,如果不添加该配置,此线条不会跑