将本地Fanout交换与芹菜而不是广播队列一起使用

时间:2019-06-24 05:25:08

标签: python celery

我正在尝试将一个作业发布由2个(或N个)任务处理。在RabbitMQ中,这通常是通过扇出交换完成的。

芹菜有可能吗?

这是我当前的设置:

from celery import Celery
from kombu import Queue, Exchange
from time import sleep

app = Celery('hello', broker='amqp://guest@localhost//')

e = Exchange('hell', type='fanout')

app.conf.task_queues = (
    Queue('default', routing_key='#'),
    Queue('wow', exchange=e, routing_key='feed.#'),
    Queue('great', exchange=e, routing_key='feed.#'),
)


app.conf.task_routes = {
    'hello': {
        'queue': 'wow',
    },
    'hello2': {
        'queue': 'great'
    },
}

@app.task(bind=True, queue='wow')
def hello(self, id):
    print("hello: %s" % id)
    return 1

@app.task(bind=True, )
def hello2(self, id):
    print("hello2: %s" % id)
    return 2

然后从其他地方呼叫:

from test1 import hello

hello.apply_async(args=(0,), exchange='hell', routing_key='feed.test')

上面的代码导致Hello处理两次相同的任务。我认为这很有意义,但似乎完全忽略了我的交换和路由键设置。 (甚至还有一点要在芹菜中设置交换和路由密钥吗?)

我还尝试过直接通过amqp客户端发布:

from test1 import app
with app.producer_pool.acquire(block=True) as producer:
       producer.publish(
            {"id": 1},
            routing_key="feed.test",
            exchange="hell",
            retry=True,
       )

但是收到以下消息:

[2019-06-23 22:24:48,180: WARNING/MainProcess] Received and deleted unknown message.  Wrong destination?!?

The full contents of the message body was: body: {'id': 1} (9b)
{content_type:'application/json' content_encoding:'utf-8'
  delivery_info:{'consumer_tag': 'None5', 'delivery_tag': 1, 'redelivered': False, 'exchange': 'hell', 'routing_key': 'feed.test'} headers={}}

我如何一次将消息发布到扇出交易所,并且必须由diff任务处理而无需第三队列?

此外,不确定我是否真的了解tasks_routes。是在接收任务时还是在发布任务时路由任务?

0 个答案:

没有答案
相关问题