<s> from __future__ import absolute_import , unicode_literals <EOL> from kombu import Connection , Producer , Consumer , Queue <EOL> from kombu . utils import uuid <EOL> class FibonacciRpcClient ( object ) : <EOL> def __init__ ( self , connection ) : <EOL> self . connection = connection <EOL> self . callback_queue = Queue ( uuid ( ) , exclusive = True , auto_delete = True ) <EOL> def on_response ( self , message ) : <EOL> if message . properties [ '<STR_LIT>' ] == self . correlation_id : <EOL> self . response = message . payload [ '<STR_LIT:result>' ] <EOL> def call ( self , n ) : <EOL> self . response = None <EOL> self . correlation_id = uuid ( ) <EOL> with Producer ( self . connection ) as producer : <EOL> producer . publish ( <EOL> { '<STR_LIT:n>' : n } , <EOL> exchange = '<STR_LIT>' , <EOL> routing_key = '<STR_LIT>' , <EOL> declare = [ self . callback_queue ] , <EOL> reply_to = self . callback_queue , <EOL> correlation_id = self . correlation_id , <EOL> ) <EOL> with Consumer ( self . connection , <EOL> on_message = self . on_response , <EOL> queues = [ self . callback_queue ] , no_ack = True ) : <EOL> while self . response is None : <EOL> self . connection . drain_events ( ) <EOL> return self . response <EOL> def main ( broker_url ) : <EOL> connection = Connection ( broker_url ) <EOL> fibonacci_rpc = FibonacciRpcClient ( connection ) <EOL> print ( '<STR_LIT>' ) <EOL> response = fibonacci_rpc . call ( <NUM_LIT:30> ) <EOL> print ( '<STR_LIT>' . format ( response ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( '<STR_LIT>' ) </s>
