2
2
import pika
3
3
import uuid
4
4
5
+
5
6
class FibonacciRpcClient (object ):
7
+
6
8
def __init__ (self ):
7
- self .connection = pika .BlockingConnection (pika . ConnectionParameters (
8
- host = 'localhost' ))
9
+ self .connection = pika .BlockingConnection (
10
+ pika . ConnectionParameters ( host = 'localhost' ))
9
11
10
12
self .channel = self .connection .channel ()
11
13
12
- result = self .channel .queue_declare (exclusive = True )
14
+ result = self .channel .queue_declare ('' , exclusive = True )
13
15
self .callback_queue = result .method .queue
14
16
15
- self .channel .basic_consume (self .on_response , no_ack = True ,
16
- queue = self .callback_queue )
17
+ self .channel .basic_consume (
18
+ queue = self .callback_queue ,
19
+ on_message_callback = self .on_response ,
20
+ auto_ack = True )
17
21
18
22
def on_response (self , ch , method , props , body ):
19
23
if self .corr_id == props .correlation_id :
@@ -22,17 +26,19 @@ def on_response(self, ch, method, props, body):
22
26
def call (self , n ):
23
27
self .response = None
24
28
self .corr_id = str (uuid .uuid4 ())
25
- self .channel .basic_publish (exchange = '' ,
26
- routing_key = 'rpc_queue' ,
27
- properties = pika .BasicProperties (
28
- reply_to = self .callback_queue ,
29
- correlation_id = self .corr_id ,
30
- ),
31
- body = str (n ))
29
+ self .channel .basic_publish (
30
+ exchange = '' ,
31
+ routing_key = 'rpc_queue' ,
32
+ properties = pika .BasicProperties (
33
+ reply_to = self .callback_queue ,
34
+ correlation_id = self .corr_id ,
35
+ ),
36
+ body = str (n ))
32
37
while self .response is None :
33
38
self .connection .process_data_events ()
34
39
return int (self .response )
35
40
41
+
36
42
fibonacci_rpc = FibonacciRpcClient ()
37
43
38
44
print (" [x] Requesting fib(30)" )
0 commit comments