【发布时间】:2015-02-26 19:14:33
【问题描述】:
我正在尝试使用 Pika 发布消息,使用 Celery 任务。
from celery import shared_task
from django.conf import settings
import json
@shared_task
def publish_message():
params = pika.URLParameters(settings.BROKER_URL + '?' + 'socket_timeout=10&' + 'connection_attempts=2')
conn = pika.BlockingConnection(parameters=params)
channel = conn.channel()
channel.exchange_declare(
exchange = 'foo',
type='topic'
)
channel.tx_select()
channel.basic_publish(
exchange = 'foo',
routing_key = 'bar',
body = json.dumps({'foo':'bar'}),
properties = pika.BasicProperties(content_type='application/json')
)
channel.tx_commit()
conn.close()
从视图中调用此任务。
由于一些奇怪的原因,有时是随机的,消息没有排队。就我而言,每秒钟的消息都会被丢弃。我在这里错过了什么?
【问题讨论】:
-
你为什么用
exchange_declare? -
发消息不需要声明exchange?这会影响这个吗?
-
你想简单地将
json.dumps({'foo':'bar'})放入队列foo吗? -
是的。我删除了
exchange_declare。一切正常。但消息仍然被丢弃。