为什么CELERY_ROUTES都有两个“队列"?和一个"routing_key"? [英] Why do CELERY_ROUTES have both a "queue" and a "routing_key"?

查看:765
本文介绍了为什么CELERY_ROUTES都有两个“队列"?和一个"routing_key"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对AMQP的理解是,消息仅包含以下组成部分:

My understanding of AMQP is that messages only have the following components:

  1. 邮件正文
  2. 路由键
  3. 交流

队列附加到交换机.消息对队列一无所知.它们只是发布到一个交换,然后根据交换类型和路由密钥,将邮件路由到一个或多个队列.

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.

在Celery中,建议的路由任务方式是通过CELERY_ROUTES设置.在文档中,CELERY_ROUTES是...

In Celery, the recommended way of routing tasks is through the CELERY_ROUTES setting. From the docs, CELERY_ROUTES is...

路由器列表,或用于将任务路由到队列的单个路由器. http://celery.readthedocs.org/en/latest/configuration.html #message-routing

A list of routers, or a single router used to route tasks to queues. http://celery.readthedocs.org/en/latest/configuration.html#message-routing

其中包括一个示例...

And it includes an example...

要将任务路由到feed_tasks队列,您可以在 CELERY_ROUTES设置:

To route a task to the feed_tasks queue, you can add an entry in the CELERY_ROUTES setting:

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

但是请稍等-根据AMQP,消息仅带有路由密钥!那里的队列"到底在干什么?

But wait a minute -- According to AMQP, messages only come with a routing key! What the heck is the "queue" doing there?

此外,还有一个默认队列的概念.如果您调用的任务未被CELERY_ROUTES捕获,它会退回到CELERY_DEFAULT_QUEUE.但同样-在AMQP中,消息不了解队列.那不是默认的路由密钥吗?

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. But again -- in AMQP, messages don't know about queues. Shouldn't that be the default routing key instead?

推荐答案

是的,当您进入Queues时,在Celery上确实有些混乱,必须记住的一件事是,queue参数是指Celery Kombu队列对象而不是直接进入AMQP队列,可以通过阅读从文档中提取. 当然,芹菜创建队列并以相同的名称进行交换的事实是混淆使用队列参数的根源. 您始终可以在文档中阅读以下段落:

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. 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'),
)

因此,您可以在同一交换机上绑定2个不同的队列. 仅使用交换器和密钥来路由任务之后,可以使用Routers类

So in this way you could bind 2 different queue on the same exchange. After to route the task using only the exchange and the key you could use Routers class

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

更多此处 http://celery.readthedocs.org/en/Latest/userguide/routing.html#routers

这篇关于为什么CELERY_ROUTES都有两个“队列"?和一个"routing_key"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆