2016-06-27 18 views
0

スクリプトから直接メッセージを送信し、処理して結果を返信したいと思います。 だから、それは二重の出版 - 購読のようなものです。RabbitMQでメッセージを受信して​​から処理し、結果を返信してください

私は2つのスクリプトがあります:クライアントはプロセサに(単純な文字列)に直接メッセージを送信し、プロセサスクリプトは中の文字を数えるよりも、

  • プロセサ
  • クライアント

を結果をクライアントに送り返します。 、

ザ・プロセサはメッセージを待つ何かを計算し、元の送信者への回答より:

は、これは私がしようとした方法です。

#Processer.py: 
import pika 
import sys 

#Sends back the score 
#addr: Connection address 
#exchName: Exchange name (where to send) 
#rKey: Name of the queue for direct messages 
#score: The detected score 
def SendActualScore(addr, exchName, rKey, score): 
    #Send the image thru the created channel with the given routing key (queue name) 
    channel.basic_publish(exchange=exchName, routing_key=rKey, body=score) 
    print "(*) Sent: " + score 

#When we receive something this is called 
def CallbackImg(ch, method, properties, body): 
    print "(*) Received: " + str(body) 
    score = str(len(body)) 
    #Send back the score 
    SendActualScore('localhost', 'valami', rKey, score) 


#Subscribe connection 
#Receive messages thru this 
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) 
channel = connection.channel() 
#RECEIVE MESSAGES - Subscribe 
channel.exchange_declare(exchange='valami', type='direct') 
#Define a queue, where we don't need the name 
#After we disconnected delete the queue (exclusive flag) 
result = channel.queue_declare(exclusive=True) 
#We need the name of our temporary queue 
queue_name = result.method.queue 

rKeys = sys.argv[1:] 
for rKey in rKeys: 
    channel.queue_bind(exchange='valami', queue=queue_name, routing_key = rKey) 

channel.basic_consume(CallbackImg, queue=queue_name, no_ack=True) 
print(' [*] Waiting for messages. To exit press CTRL+C') 
channel.start_consuming() 

クライアントはメッセージを送信し、応答を待つだけです。

#Client.py: 
import pika 
import sys 

connAddr = 'localhost' 

#Establish connection 
connection = pika.BlockingConnection(pika.ConnectionParameters(connAddr)) 
channel = connection.channel() 

#Define an exchange channel, we don't need a queue 
channel.exchange_declare(exchange='valami', type='direct') 

#Send the image thru the created channel 
channel.basic_publish(exchange='valami', routing_key='msg', body='Message in the body') 

print "[*] Sent" 

def Callback(ch, method, properties, body): 
    print "(*) Received: " + str(body) 

result = channel.queue_declare(exclusive=True) 
#We need the name of our temporary queue 
queue_name = result.method.queue 

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

channel.basic_consume(Callback, queue=queue_name, no_ack=True) 
print(' [*] Waiting for messages. To exit press CTRL+C') 
channel.start_consuming() 

複数のクライアントが存在する可能性があり、メッセージを直接それらに返信する方法がわかりません。

答えて

0

RabbitMQ w/pythonとpikaでRPCのチュートリアルを確認しましたか? http://www.rabbitmq.com/tutorials/tutorial-six-python.html


あなたがあなたのクライアントに何をする必要があるかの要旨は、しかし、いくつかの変更と、RPCのチュートリアルで発見されました。

クライアントでは、サーバーで行ったのと同じ方法で排他的キューを作成する必要があります。

あなたがクライアントからメッセージを送信すると、あなたはチュートリアルから、クライアントの排他的なキュー

の名前にreply_toを設定する必要があります:あなたが受け取る際に、サーバー上で

channel.basic_publish(exchange='', 
         routing_key='rpc_queue', 
         properties=pika.BasicProperties(
          reply_to = callback_queue, 
          ), 
         body=request) 

メッセージからメッセージのreply_toヘッダーを読み取ってから、basic_publishそのキューへの応答を読み取る必要があります。


むしろ「クライアント」と「サーバ」を考えるよりも、「メッセージプロデューサ」と「メッセージコンシューマ」の用語でこれをフレームに役立つかもしれません。

シナリオでは、両方のプロセスがパブリッシャーとコンシューマーの両方である必要があります。 「クライアント」は元のメッセージを公開し、応答を消費します。 「サーバー」は元のメッセージを消費し、応答を発行します。

コードの唯一の違いは、元のメッセージにreply_toヘッダーを使用することだけです。これは、応答を公開するキューの名前です。

希望に役立ちます!


P.S.私はRabbitMQ Patterns eBookの中核となるこの概要をカバーしています。あなたが必要としているようにRPCと要求/応答の両方があります。この本は、特定のプログラミング言語ではなく、原則とパターンで話します(私は主にnode.jsを書きますが、実際にはPythonは知らない)。

+0

はい私はしましたが、私はこの作業をpub-subで行う必要があります。 – Gabe

関連する問題