我有一个简单的应用程序,它发送和接收消息、kombu,并使用 Celery 来处理消息。Kombu alon,我可以正确接收消息。当我发送“Hello”时,kombu 会收到“Hello”。但是当我添加任务时,kombu 收到的是芹菜的任务 ID。
我对这个项目的目的是让我可以安排何时发送和接收消息,因此是 Celery。
我想知道的是为什么kombu会收到任务ID而不是发送的消息?我找了又找,没有找到关于这件事的任何相关结果。我是使用此应用程序的初学者,我希望能在解决此问题方面提供一些帮助。
我的代码:
任务.py
from celery import Celery
app = Celery('tasks', broker='amqp://xx:xx@localhost/xx', backend='amqp://')
@app.task(name='task.add')
def add(x, y):
return x+y
发送.py
import kombu
from task import add
#declare connection with broker connection
connection = kombu.Connection(hostname='xx',
userid='xx',
password='xx',
virtual_host='xx')
connection.connect()
if connection.connect() is False:
print("not connected")
else:
print("connected")
#checks if connection is okay
#rabbitmq connection
channel = connection.channel()
#queue & exchange for kombu
exchange = kombu.Exchange('exchnge', type='direct')
queue = kombu.Queue('kombu_queue', exchange, routing_key='queue1')
#message here
x = input ("Enter first name: ")
y = input ("Enter last name: ")
result= add.delay(x,y)
print(result)
#syntax used for sending messages to queue
producer = kombu.Producer(channel, exchange)
producer.publish(result,
exchange = exchange,
routing_key='queue1')
print("Message sent: [x]")
connection.release()
接收.py
import kombu
#receive
connection = kombu.Connection(hostname='xx',
userid='xx',
password='xx',
virtual_host='xx')
connection.connect()
channel = connection.channel()
exchange = kombu.Exchange('exchnge', type='direct')
queue = kombu.Queue('kombu_queue', exchange, routing_key='queue1')
print("Waiting for messages...")
def callback(body, message):
print('Got message - %s' % body)
message.ack()
consumer = kombu.Consumer(channel,
queues=queue,
callbacks=[callback])
consumer.consume()
while True:
connection.drain_events()
我在用:
Kombu 3.0.26
Celery 3.1.18
RabbitMQ as the broker
我发送的内容:
xxx
yyy
什么kombu收到:
Got message - d22880c9-b22c-48d8-bc96-5d839b224f2a