minhasapov_ruslan_lab_4 #209
28
minhasapov_ruslan_lab_4/labwork/consumer_1.py
Normal file
28
minhasapov_ruslan_lab_4/labwork/consumer_1.py
Normal file
@ -0,0 +1,28 @@
|
||||
import pika
|
||||
import time
|
||||
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(f"[x] Consumer 1 received: {body.decode()}")
|
||||
|
||||
time.sleep(3)
|
||||
|
||||
print(f"[x] Consumer 1 processed:: {body.decode()}")
|
||||
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||
|
||||
|
||||
def consume():
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', port=5672))
|
||||
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='consumer_1_queue')
|
||||
channel.queue_bind(exchange='server_monitoring', queue='consumer_1_queue')
|
||||
|
||||
channel.basic_consume(queue='consumer_1_queue', on_message_callback=callback)
|
||||
|
||||
print(' [*] Consumer 1 waiting for messages. To exit press CTRL+C')
|
||||
channel.start_consuming()
|
||||
|
||||
if __name__ == "__main__":
|
||||
consume()
|
26
minhasapov_ruslan_lab_4/labwork/consumer_2.py
Normal file
26
minhasapov_ruslan_lab_4/labwork/consumer_2.py
Normal file
@ -0,0 +1,26 @@
|
||||
import pika
|
||||
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(f"[x] Consumer 2 received: {body.decode()}")
|
||||
|
||||
print(f"[x] Consumer 2 processed:: {body.decode()}")
|
||||
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||
|
||||
|
||||
def consume():
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', port=5672))
|
||||
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='consumer_2_queue')
|
||||
channel.queue_bind(exchange='server_monitoring', queue='consumer_2_queue')
|
||||
|
||||
channel.basic_consume(queue='consumer_2_queue', on_message_callback=callback)
|
||||
|
||||
print(' [*] Consumer 2 waiting for messages. To exit press CTRL+C')
|
||||
channel.start_consuming()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
consume()
|
28
minhasapov_ruslan_lab_4/labwork/publisher.py
Normal file
28
minhasapov_ruslan_lab_4/labwork/publisher.py
Normal file
@ -0,0 +1,28 @@
|
||||
import pika
|
||||
import time
|
||||
import random
|
||||
|
||||
def generage_message_metrics():
|
||||
return f" | CPU: {random.randint(0, 100)}% | RAM: {random.randint(0, 100)}% | CD: {random.randint(0, 100)}% |"
|
||||
|
||||
def produce():
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', port=5672))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.exchange_declare(exchange='server_monitoring', exchange_type='fanout')
|
||||
|
||||
messages = [
|
||||
"Server | ID: 1",
|
||||
"Server | ID: 2",
|
||||
"Server | ID: 3",
|
||||
]
|
||||
|
||||
while True:
|
||||
for message in messages:
|
||||
message_with_metric = message + generage_message_metrics()
|
||||
channel.basic_publish(exchange='server_monitoring', routing_key='', body=message_with_metric)
|
||||
print(f"[x] Sent: {message_with_metric}")
|
||||
time.sleep(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
produce()
|
BIN
minhasapov_ruslan_lab_4/pictures/lab_con1&2.png
Normal file
BIN
minhasapov_ruslan_lab_4/pictures/lab_con1&2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 92 KiB |
BIN
minhasapov_ruslan_lab_4/pictures/lab_con1&many.png
Normal file
BIN
minhasapov_ruslan_lab_4/pictures/lab_con1&many.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 75 KiB |
BIN
minhasapov_ruslan_lab_4/pictures/tutor_1.png
Normal file
BIN
minhasapov_ruslan_lab_4/pictures/tutor_1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 49 KiB |
BIN
minhasapov_ruslan_lab_4/pictures/tutor_2.png
Normal file
BIN
minhasapov_ruslan_lab_4/pictures/tutor_2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 44 KiB |
BIN
minhasapov_ruslan_lab_4/pictures/tutor_3.png
Normal file
BIN
minhasapov_ruslan_lab_4/pictures/tutor_3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 38 KiB |
28
minhasapov_ruslan_lab_4/tutor_1/receiver.py
Normal file
28
minhasapov_ruslan_lab_4/tutor_1/receiver.py
Normal file
@ -0,0 +1,28 @@
|
||||
import os
|
||||
import sys
|
||||
import pika
|
||||
|
||||
|
||||
def main():
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='hello')
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(f" [x] Received {body}")
|
||||
|
||||
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
|
||||
|
||||
print(' [*] Waiting for messages. To exit press CTRL+C')
|
||||
channel.start_consuming()
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except KeyboardInterrupt:
|
||||
print('Interrupted')
|
||||
try:
|
||||
sys.exit(0)
|
||||
except SystemExit:
|
||||
os._exit(0)
|
13
minhasapov_ruslan_lab_4/tutor_1/send.py
Normal file
13
minhasapov_ruslan_lab_4/tutor_1/send.py
Normal file
@ -0,0 +1,13 @@
|
||||
import pika
|
||||
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='hello')
|
||||
|
||||
channel.basic_publish(exchange='',
|
||||
routing_key='hello',
|
||||
body='Hello World!')
|
||||
print(" [x] Sent 'Hello World!'")
|
||||
|
||||
connection.close()
|
19
minhasapov_ruslan_lab_4/tutor_2/new_task.py
Normal file
19
minhasapov_ruslan_lab_4/tutor_2/new_task.py
Normal file
@ -0,0 +1,19 @@
|
||||
import pika
|
||||
import sys
|
||||
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='task_queue', durable=True)
|
||||
|
||||
message = ' '.join(sys.argv[1:]) or "Hello World!"
|
||||
channel.basic_publish(
|
||||
exchange='',
|
||||
routing_key='task_queue',
|
||||
body=message,
|
||||
properties=pika.BasicProperties(
|
||||
delivery_mode=pika.DeliveryMode.Persistent
|
||||
))
|
||||
print(f" [x] Sent {message}")
|
||||
connection.close()
|
22
minhasapov_ruslan_lab_4/tutor_2/worker.py
Normal file
22
minhasapov_ruslan_lab_4/tutor_2/worker.py
Normal file
@ -0,0 +1,22 @@
|
||||
import pika
|
||||
import time
|
||||
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='task_queue', durable=True)
|
||||
print(' [*] Waiting for messages. To exit press CTRL+C')
|
||||
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(f" [x] Received {body.decode()}")
|
||||
time.sleep(body.count(b'.'))
|
||||
print(" [x] Done")
|
||||
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||
|
||||
|
||||
channel.basic_qos(prefetch_count=1)
|
||||
channel.basic_consume(queue='task_queue', on_message_callback=callback)
|
||||
|
||||
channel.start_consuming()
|
13
minhasapov_ruslan_lab_4/tutor_3/emit_log.py
Normal file
13
minhasapov_ruslan_lab_4/tutor_3/emit_log.py
Normal file
@ -0,0 +1,13 @@
|
||||
import pika
|
||||
import sys
|
||||
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.exchange_declare(exchange='logs', exchange_type='fanout')
|
||||
|
||||
message = ' '.join(sys.argv[1:]) or "info: Hello World!"
|
||||
channel.basic_publish(exchange='logs', routing_key='', body=message)
|
||||
print(f" [x] Sent {message}")
|
||||
connection.close()
|
22
minhasapov_ruslan_lab_4/tutor_3/receive_logs.py
Normal file
22
minhasapov_ruslan_lab_4/tutor_3/receive_logs.py
Normal file
@ -0,0 +1,22 @@
|
||||
import pika
|
||||
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.exchange_declare(exchange='logs', exchange_type='fanout')
|
||||
|
||||
result = channel.queue_declare(queue='', exclusive=True)
|
||||
queue_name = result.method.queue
|
||||
|
||||
channel.queue_bind(exchange='logs', queue=queue_name)
|
||||
|
||||
print(' [*] Waiting for logs. To exit press CTRL+C')
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(f" [x] {body}")
|
||||
|
||||
channel.basic_consume(
|
||||
queue=queue_name, on_message_callback=callback, auto_ack=True)
|
||||
|
||||
channel.start_consuming()
|
Loading…
Reference in New Issue
Block a user