在Django中使用Kombu的正确方法是什么?这是我的工作代码,位于app_name/tasks.py
:
import json
from kombu import Connection, Producer, Exchange
from django.conf import settings
from celery import shared_task
@shared_task
def update_metadata(message, *args, **kwargs):
print(message)
message_dict = json.loads(message)
# process the message
# convert it into consumable format?
# update the local file
publish_metadata(message_dict)
def publish_metadata(message):
"""Puts the metadata in the outbound queues
"""
connection = Connection(settings.BROKER_URL)
connection.connect()
exchange = Exchange(name=settings.OUTBOUND_EXCHANGE, type='fanout')
producer = Producer(channel=connection, serializer='json',
exchange=exchange)
producer.publish(body=message, serializer='json')
但每次想要发布消息时,它都会与交换机建立新的连接。另外,我想在publish_metadata
中使用Kombu,而不是Celery。我想将消息推送到队列,而消费者不是Celery。所以我不能使用芹菜,因为芹菜会给身体增加很多参数和东西。