|
@@ -3,43 +3,47 @@
|
|
|
import pika
|
|
|
import uuid
|
|
|
|
|
|
+
|
|
|
class FibonacciRpcClient(object):
|
|
|
def __init__(self):
|
|
|
self.connection = pika.BlockingConnection(
|
|
|
- pika.ConnectionParameters(host='localhost'))
|
|
|
+ pika.ConnectionParameters(host="localhost")
|
|
|
+ )
|
|
|
self.channel = self.connection.channel()
|
|
|
|
|
|
- result = self.channel.queue_declare(queue='', exclusive=True)
|
|
|
+ result = self.channel.queue_declare(queue="", exclusive=True)
|
|
|
self.callback_queue = result.method.queue
|
|
|
|
|
|
self.channel.basic_consume(
|
|
|
queue=self.callback_queue,
|
|
|
on_message_callback=self.on_response,
|
|
|
- auto_ack=True)
|
|
|
+ auto_ack=True,
|
|
|
+ )
|
|
|
|
|
|
def on_response(self, ch, method, props, body):
|
|
|
if self.corr_id == props.correlation_id:
|
|
|
self.response = body
|
|
|
-
|
|
|
- def call(self,n):
|
|
|
+
|
|
|
+ def call(self, n):
|
|
|
self.response = None
|
|
|
self.corr_id = str(uuid.uuid4())
|
|
|
self.channel.basic_publish(
|
|
|
- exchange='',
|
|
|
- routing_key='rpc_queue',
|
|
|
+ exchange="",
|
|
|
+ routing_key="rpc_queue",
|
|
|
properties=pika.BasicProperties(
|
|
|
- reply_to=self.callback_queue,
|
|
|
- correlation_id=self.corr_id,
|
|
|
+ reply_to=self.callback_queue, correlation_id=self.corr_id
|
|
|
),
|
|
|
- body=str(n)
|
|
|
+ body=str(n),
|
|
|
)
|
|
|
while self.response is None:
|
|
|
self.connection.process_data_events()
|
|
|
return int(self.response)
|
|
|
|
|
|
+
|
|
|
fibonacci_rpc = FibonacciRpcClient()
|
|
|
|
|
|
-for n in range(10,30, 5):
|
|
|
+
|
|
|
+for n in range(10, 30, 5):
|
|
|
print(" [x] Requesting fib({0})".format(n))
|
|
|
response = fibonacci_rpc.call(n)
|
|
|
print(" [.] Got %r" % response)
|