メッセージ、昆布を送受信し、セロリを使用してメッセージを処理する単純なアプリケーションがあります。昆布だけでちゃんとメッセージを受け取れます。私が「こんにちは」を送ると、昆布は「こんにちは」を受け取ります。しかし、タスクを追加すると、kombu が受け取るのはセロリのタスク ID です。
このプロジェクトの私の目的は、メッセージをいつ送受信するかをスケジュールできるようにすることです。したがって、Celery です。
私が知りたいのは、kombu が送信されたメッセージではなくタスク ID を受け取るのはなぜですか? 私は検索して検索しましたが、この問題に関連する結果は見つかりませんでした。私はこのアプリケーションを使用する初心者であり、この問題を解決するための助けをいただければ幸いです。
私のコード:
task.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
send.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()
receive.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
昆布が受け取るもの:
Got message - d22880c9-b22c-48d8-bc96-5d839b224f2a