45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
import threading
|
|
|
|
import pika
|
|
import json
|
|
|
|
|
|
class LectureConsumer2:
|
|
def __init__(self):
|
|
self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
|
self.channel = self.connection.channel()
|
|
|
|
self.channel.exchange_declare(exchange='lecture_exchange', exchange_type='fanout')
|
|
|
|
result = self.channel.queue_declare(queue='', exclusive=True)
|
|
self.queue_name = result.method.queue
|
|
print('fast + ' + self.queue_name)
|
|
|
|
self.channel.queue_bind(exchange='lecture_exchange', queue=self.queue_name)
|
|
|
|
def callback(self, ch, method, properties, body):
|
|
lecture_event = json.loads(body)
|
|
print(f" [x] Consumer_fast received and processed lecture event instantly: {lecture_event}")
|
|
|
|
def consume_lectures(self):
|
|
self.channel.basic_consume(queue=self.queue_name, on_message_callback=self.callback)
|
|
print(' [*] Consumer_fast waiting for lecture events. To exit press CTRL+C')
|
|
self.channel.start_consuming()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
lecture_consumer2 = LectureConsumer2()
|
|
try:
|
|
lecture_consumer2.consume_lectures()
|
|
except KeyboardInterrupt:
|
|
lecture_consumer2.connection.close()
|
|
|
|
# if __name__ == '__main__':
|
|
# lecture_consumer2 = LectureConsumer2()
|
|
# try:
|
|
# t1 = threading.Thread(target=lecture_consumer2.consume_lectures)
|
|
# t1.start()
|
|
# t1.join()
|
|
# except KeyboardInterrupt:
|
|
# lecture_consumer2.connection.close()
|