import pika import uuid from functions import log class ResearchProposal(object): def __init__(self): self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password"))) self.channel = self.connection.channel() 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) self.response = None self.corr_id = None def on_response(self, ch, method, properties, body): if self.corr_id == properties.correlation_id: self.response = body def call(self, n): self.response = None self.corr_id = str(uuid.uuid4()) self.channel.basic_publish(exchange='', routing_key="research_proposals", properties=pika.BasicProperties(reply_to=self.callback_queue, correlation_id=self.corr_id, ), body=f"{n['acronym']},{n['title']},{n['description']},{n['amount']}") self.connection.process_data_events(time_limit=None) return str(self.response) proposals = [ {"acronym": "PROJ1", "title": "Project 1", "description": "Description 1", "amount": "300000"}, {"acronym": "PROJ2", "title": "Project 2", "description": "Description 2", "amount": "400000"}, {"acronym": "PROJ3", "title": "Project 3", "description": "Description 3", "amount": "250000"}, {"acronym": "PROJ4", "title": "Project 4", "description": "Description 4", "amount": "100000"}, {"acronym": "PROJ5", "title": "Project 5", "description": "Description 5", "amount": "900000"}, {"acronym": "PROJ6", "title": "Project 6", "description": "Description 6", "amount": "420000"} ] for proposal in proposals: proposal_client = ResearchProposal() log(f"{proposal['acronym']} sends proposal") response = proposal_client.call(proposal) log(f"{proposal['acronym']} got {response}")