aboutsummaryrefslogtreecommitdiffstats
path: root/rmq/consumer.py
diff options
context:
space:
mode:
authorGravatar Christopher Baklid <[email protected]>2018-05-23 22:38:20 +0200
committerGravatar Christopher Baklid <[email protected]>2018-05-23 22:38:20 +0200
commit309a6f93f878fc96951902fc47d45a30ef5f8d71 (patch)
treef788b43a892a93d0f97da73f459a55b43e1ea1a0 /rmq/consumer.py
parentupdate readme (diff)
POC completed
Diffstat (limited to 'rmq/consumer.py')
-rw-r--r--rmq/consumer.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/rmq/consumer.py b/rmq/consumer.py
new file mode 100644
index 0000000..8e1ce2b
--- /dev/null
+++ b/rmq/consumer.py
@@ -0,0 +1,35 @@
+import time
+import traceback
+import pika
+from pika.exceptions import ConnectionClosed
+
+def consume(username='guest', password='guest', host='localhost', port=5672, queue='', callback=None):
+ while True:
+ credentials = pika.PlainCredentials(username, password)
+ con_params = pika.ConnectionParameters(host, port, '/', credentials)
+
+ try:
+ connection = pika.BlockingConnection(con_params)
+
+ try:
+ channel = connection.channel()
+ channel.queue_declare(queue=queue, durable=False)
+ channel.basic_qos(prefetch_count=1)
+ channel.basic_consume(callback, queue=queue)
+
+ print(f"""Connected to host: {host} port: {port} queue: {queue}""", flush=True)
+
+ channel.start_consuming()
+
+ except:
+ exc = traceback.format_exc()
+ print(exc, flush=True)
+
+ finally:
+ connection.close()
+
+ except ConnectionClosed:
+ print(f"Connection lost, reconnecting to {host}", flush=True)
+ pass
+
+ time.sleep(2)