简体   繁体   English

为什么CELERY_ROUTES同时具有“队列”和“routing_key”?

[英]Why do CELERY_ROUTES have both a “queue” and a “routing_key”?

My understanding of AMQP is that messages only have the following components: 我对AMQP的理解是消息只有以下组件:

  1. The message body 消息体
  2. The routing key 路由密钥
  3. The exchange 找的零钱

Queues are attached to exchanges. 队列附加到交易所。 Messages can't have any knowledge of queues. 消息不能具有任何队列知识。 They just post to an exchange, and then based on the exchange type and routing key, the messages are routed to one or more queues. 它们只是发布到交换机,然后根据交换类型和路由密钥,消息被路由到一个或多个队列。

In Celery, the recommended way of routing tasks is through the CELERY_ROUTES setting. 在Celery中,推荐的路由任务方法是通过CELERY_ROUTES设置。 From the docs, CELERY_ROUTES is... 从文档来看, CELERY_ROUTES是......

A list of routers, or a single router used to route tasks to queues. 路由器列表,或用于将任务路由到队列的单个路由器。 http://celery.readthedocs.org/en/latest/configuration.html#message-routing http://celery.readthedocs.org/en/latest/configuration.html#message-routing

And it includes an example... 它包括一个例子......

To route a task to the feed_tasks queue, you can add an entry in the CELERY_ROUTES setting: 要将任务路由到feed_tasks队列,可以在CELERY_ROUTES设置中添加一个条目:

 CELERY_ROUTES = { 'feeds.tasks.import_feed': { 'queue': 'feed_tasks', 'routing_key': 'feed.import', }, } 

But wait a minute -- According to AMQP, messages only come with a routing key! 但是等一下 - 根据AMQP,消息只带有路由密钥! What the heck is the "queue" doing there? 什么是“排队”在那里做什么?

Furthermore, there's this notion of a default queue. 此外,还有一个默认队列的概念。 If you invoke a task which isn't caught by CELERY_ROUTES , it falls back to CELERY_DEFAULT_QUEUE . 如果您调用未被CELERY_ROUTES捕获的任务,则它将回CELERY_DEFAULT_QUEUE But again -- in AMQP, messages don't know about queues. 但是再次 - 在AMQP中,消息不知道队列。 Shouldn't that be the default routing key instead? 这不应该是默认的路由密钥吗?

Is true that on Celery there is a bit of confusion when you go to Queues, one thing you must keep in mind is that queue parameter refers to a Celery Kombu Queue Object and not directly to a AMQP queue, you can understand this by reading this extract from the docs . 确实,在Celery上,当你去Queues时会有一些混乱,你必须记住的一件事是队列参数是指Celery Kombu队列对象而不是直接到AMQP队列,你可以通过阅读这个来理解这一点从文档中提取 Of course the fact that celery creates the queue and exchange with the same name is the origin of confusion of the usage of queue parameter. 当然,芹菜创建队列并使用相同名称进行交换的事实是队列参数使用混乱的根源。 Always in the docs you can read this paragraph: 始终在文档中,您可以阅读本段:

If you have another queue but on another exchange you want to add, just specify a custom exchange and exchange type: 如果您有另一个队列,但在另一个要添加的交换机上,只需指定自定义交换和交换类型:

 CELERY_QUEUES = ( Queue('feed_tasks', routing_key='feed.#'), Queue('regular_tasks', routing_key='task.#'), Queue('image_tasks', exchange=Exchange('mediatasks', type='direct'), routing_key='image.compress'), ) 

So in this way you could bind 2 different queue on the same exchange. 所以这样你可以在同一个交换机上绑定2个不同的队列。 After to route the task using only the exchange and the key you could use Routers class 在仅使用交换机和密钥路由任务后,您可以使用Routers类

class MyRouter(object):

    def route_for_task(self, task, args=None, kwargs=None):
        if task == 'myapp.tasks.compress_video':
            return {'exchange': 'video',
                    'exchange_type': 'topic',
                    'routing_key': 'video.compress'}
        return None

More here http://celery.readthedocs.org/en/latest/userguide/routing.html#routers 更多这里http://celery.readthedocs.org/en/latest/userguide/routing.html#routers

The point of having the queue declared in there, is for celery to create those queues and set up the configuration with RabbitMQ. 在那里声明队列的要点是芹菜创建这些队列并使用RabbitMQ设置配置。

For a lower level AMQP client, you need to first declare the queue, then the exchange, and then finally bind the exchange to the queue. 对于较低级别的AMQP客户端,您需要首先声明队列,然后是交换,然后最终将交换绑定到队列。 Later when posting messages, you just post to the exchange. 稍后发布消息时,您只需发布到交易所。

It seems like celery use this structure to do it automatically. 似乎芹菜使用这种结构自动完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM