ca4006-ca2/researcher.py

38 lines
1.6 KiB
Python

import pika
import uuid
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password")))
channel = connection.channel()
channel.queue_declare(queue='research_proposals')
def process_response(ch, method, properties, body):
if corr_id == properties.correlation_id:
print(body)
else:
print("response failed")
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"}
]
corr_id = str(uuid.uuid4())
for proposal in proposals:
response_queue = channel.queue_declare(queue='', exclusive=True).method.queue
channel.basic_publish(exchange='',
routing_key='research_proposals',
properties=pika.BasicProperties(reply_to=response_queue, correlation_id=corr_id),
body=f"{proposal['acronym']},{proposal['title']},{proposal['description']},{proposal['amount']}")
print(f"Proposal {proposal['acronym']} sent")
channel.basic_consume(queue=response_queue, on_message_callback=process_response)
connection.close()