Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed rpc client.

  • Loading branch information...
commit f58bb9cd0f81c4eb10753162cb6804df2cd6eed5 1 parent f6a8e53
@majek majek authored
Showing with 25 additions and 20 deletions.
  1. +16 −12 python/rpc_client.py
  2. +9 −8 python/rpc_server.py
View
28 python/rpc_client.py
@@ -12,26 +12,30 @@ def __init__(self):
result = self.channel.queue_declare(auto_delete=True)
self.callback_queue = result.queue
+ self.requests = {}
+ self.channel.basic_consume(self.on_response, no_ack=True,
+ queue=self.callback_queue)
+
+ def on_response(self, ch, method, props, body):
+ corr_id = props.correlation_id
+ if corr_id in self.requests:
+ self.requests[corr_id] = body
+
def call(self, n):
- correlation_id = str(uuid.uuid4())
+ corr_id = str(uuid.uuid4())
+ self.requests[corr_id] = None
self.channel.basic_publish(exchange='',
routing_key='rpc_queue',
properties=pika.BasicProperties(
reply_to = self.callback_queue,
- correlation_id = correlation_id,
+ correlation_id = corr_id,
),
body=str(n))
- response = []
- def on_basic_deliver(ch, method, props, body):
- if props.correlation_id == correlation_id:
- response.append(body)
- self.channel.basic_consume(on_basic_deliver,
- queue=self.callback_queue,
- no_ack=True)
- while not response:
+ while self.requests[corr_id] is None:
pika.asyncore_loop(count=1)
-
- return int(response[0])
+ response = self.requests[corr_id]
+ del self.requests[corr_id]
+ return int(response)
fibonacci_rpc = FibonacciClient()
View
17 python/rpc_server.py
@@ -1,14 +1,6 @@
#!/usr/bin/env python
import pika
-def fib(n):
- if n == 0:
- return 0
- elif n == 1:
- return 1
- else:
- return fib(n-1) + fib(n-2)
-
connection = pika.AsyncoreConnection(pika.ConnectionParameters(
host='127.0.0.1',
@@ -18,6 +10,15 @@ def fib(n):
channel.queue_declare(queue='rpc_queue')
+
+def fib(n):
+ if n == 0:
+ return 0
+ elif n == 1:
+ return 1
+ else:
+ return fib(n-1) + fib(n-2)
+
def on_request(ch, method, props, body):
n = int(body)
Please sign in to comment.
Something went wrong with that request. Please try again.