bigchungusdata/rabbit/consumer.py

34 lines
1.1 KiB
Python
Raw Normal View History

2021-04-24 18:43:28 +02:00
#!/usr/bin/env python3
from datetime import datetime
import pika
DATETIMES=[]
def callback(ch, method, properties, body):
DATETIMES.append(datetime.now())
try:
credentials = pika.PlainCredentials("rabbit", "rabbit")
connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost", credentials=credentials, heartbeat=0, socket_timeout=5))
channel = connection.channel()
channel.exchange_declare(exchange="test",exchange_type='direct')
queue_declare_result = channel.queue_declare(queue='testqueue', exclusive=False)
queue_name = queue_declare_result.method.queue
channel.queue_bind(exchange="test",routing_key='test', queue=queue_name)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
channel.start_consuming()
2021-04-25 12:24:41 +02:00
except KeyboardInterrupt:
2021-04-24 18:43:28 +02:00
connection.close()
2021-04-25 13:02:15 +02:00
dt = {}
for datetime in DATETIMES:
dts = datetime.strftime("%m/%d/%Y, %H:%M:%S")
if dts not in dt:
dt[strftime("%m/%d/%Y, %H:%M:%S")] = 1
else:
dt[strftime("%m/%d/%Y, %H:%M:%S")] +=1