Cleanup & indentation fix

This commit is contained in:
Matěj Divecký 2020-09-28 18:28:26 +02:00
parent 8d89d3c47e
commit aa684bcebe

View file

@ -11,39 +11,38 @@ channel = connection.channel()
channel.queue_declare(queue='signal-receive') channel.queue_declare(queue='signal-receive')
def demo_echo(text, sender, destination): def demo_echo(text, sender, destination):
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_host)) connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_host))
channel = connection.channel() channel = connection.channel()
channel.queue_declare(queue='signal-send') channel.queue_declare(queue='signal-send')
message = [ message = [
text, text,
destination, destination,
sender sender
] ]
message_json = json.dumps(message) message_json = json.dumps(message)
channel.basic_publish(exchange='', channel.basic_publish(exchange='',
routing_key='signal-send', routing_key='signal-send',
body=message_json) body=message_json)
connection.close() connection.close()
def callback(ch, method, properties, body): def callback(ch, method, properties, body):
body = json.loads(body) body = json.loads(body)
text = body[0] text = body[0]
timestamp = body[1] timestamp = body[1]
sender = body[2] sender = body[2]
reciever = body[3] reciever = body[3]
demo_echo(text, sender, reciever) demo_echo(text, sender, reciever)
print(body)
# This could be used to distribute messages to multiple servers. Just acknowledge message only if you should. (eq. based on rec number) # This could be used to distribute messages to multiple servers. Just acknowledge message only if you should. (eq. based on rec number)
channel.basic_ack(delivery_tag=method.delivery_tag) channel.basic_ack(delivery_tag=method.delivery_tag)
@ -51,10 +50,10 @@ channel.basic_consume('signal-receive', callback, auto_ack=False)
try: try:
print("starting consuming") print("starting consuming")
channel.start_consuming() channel.start_consuming()
except KeyboardInterrupt: except KeyboardInterrupt:
channel.stop_consuming() channel.stop_consuming()
connection.close() connection.close()