首页 > 解决方案 > Rabbitmq 消费者和 celery 消费者有什么区别?

问题描述

RabbitMQ 消费者示例

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='logs', exchange_type='fanout')

result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue

channel.queue_bind(exchange='logs', queue=queue_name)

print(' [*] Waiting for logs. To exit press CTRL+C')

def callback(ch, method, properties, body):
    print(" [x] %r" % body)

channel.basic_consume(
    queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

芹菜消费者:

from celery import Celery

app = Celery('tasks', broker='pyamqp://guest@localhost//')

@app.task
def add(x, y):
    return x + y

我是新手,如果我错了,请纠正我。

我的问题是这两者之间的主要区别是什么?既等待任务又执行队列中的数据。

我的第二个问题是,我们可以将数据从 rabbitMQ 生产者推送到 rabbitmq-server 并在 Celery 消费者中接收吗?如果是请建议如何?任何参考都会有所帮助。

我试过了,但是 celery 无法理解消息 Use RabbitMQ as procedure 和 Celery as consumer

标签: pythonrabbitmqceleryproducer-consumerrabbitmq-exchange

解决方案


推荐阅读