1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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 Exception:
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)
|