Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 41 lines (32 sloc) 1.409 kb
ca18270e »
2010-11-30 Rpc examples
1 #!/usr/bin/env python
2 import pika
f6a8e532 »
2010-11-30 Json and class in the server is not required
3 import uuid
ca18270e »
2010-11-30 Rpc examples
4
c5324b1f »
2011-04-07 FibonacciRpcClient class name
5 class FibonacciRpcClient(object):
ca18270e »
2010-11-30 Rpc examples
6 def __init__(self):
596faaf0 »
2011-03-14 Python examples updated to recent pika
7 self.connection = pika.BlockingConnection(pika.ConnectionParameters(
d87f64c4 »
2010-12-13 use exclusive instead of auto-delete for #6
8 host='localhost'))
9
ca18270e »
2010-11-30 Rpc examples
10 self.channel = self.connection.channel()
11
d87f64c4 »
2010-12-13 use exclusive instead of auto-delete for #6
12 result = self.channel.queue_declare(exclusive=True)
596faaf0 »
2011-03-14 Python examples updated to recent pika
13 self.callback_queue = result.method.queue
ca18270e »
2010-11-30 Rpc examples
14
f58bb9cd »
2010-11-30 Fixed rpc client.
15 self.channel.basic_consume(self.on_response, no_ack=True,
16 queue=self.callback_queue)
17
18 def on_response(self, ch, method, props, body):
596faaf0 »
2011-03-14 Python examples updated to recent pika
19 if self.corr_id == props.correlation_id:
20 self.response = body
f58bb9cd »
2010-11-30 Fixed rpc client.
21
f6a8e532 »
2010-11-30 Json and class in the server is not required
22 def call(self, n):
c1b178d9 »
2011-08-24 Fixed bug in tutorial 6 caused by misunderstanding of stop_consuming().
23 self.response = None
596faaf0 »
2011-03-14 Python examples updated to recent pika
24 self.corr_id = str(uuid.uuid4())
ca18270e »
2010-11-30 Rpc examples
25 self.channel.basic_publish(exchange='',
26 routing_key='rpc_queue',
27 properties=pika.BasicProperties(
28 reply_to = self.callback_queue,
596faaf0 »
2011-03-14 Python examples updated to recent pika
29 correlation_id = self.corr_id,
ca18270e »
2010-11-30 Rpc examples
30 ),
f6a8e532 »
2010-11-30 Json and class in the server is not required
31 body=str(n))
c1b178d9 »
2011-08-24 Fixed bug in tutorial 6 caused by misunderstanding of stop_consuming().
32 while self.response is None:
33 self.connection.process_data_events()
596faaf0 »
2011-03-14 Python examples updated to recent pika
34 return int(self.response)
ca18270e »
2010-11-30 Rpc examples
35
c5324b1f »
2011-04-07 FibonacciRpcClient class name
36 fibonacci_rpc = FibonacciRpcClient()
ca18270e »
2010-11-30 Rpc examples
37
f6a8e532 »
2010-11-30 Json and class in the server is not required
38 print " [x] Requesting fib(30)"
39 response = fibonacci_rpc.call(30)
ca18270e »
2010-11-30 Rpc examples
40 print " [.] Got %r" % (response,)
Something went wrong with that request. Please try again.