You've already forked Curso-lenguaje-python
Add rabbitmq test
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python
|
||||
import pika
|
||||
import sys
|
||||
|
||||
|
||||
# Establecer la conexión con el servidor RabbitMQ
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
# Crear una cola llamada 'hola'
|
||||
channel.queue_declare(queue='task_queue', durable=True)
|
||||
|
||||
# Mensaje a enviar
|
||||
message = ' '.join(sys.argv[1:]) or "¡Hola mundo!"
|
||||
|
||||
# Enviar mensaje
|
||||
channel.basic_publish(
|
||||
exchange='', routing_key='task_queue', body=message,
|
||||
properties=pika.BasicProperties(
|
||||
delivery_mode=pika.DeliveryMode.Persistent
|
||||
)
|
||||
)
|
||||
|
||||
# Traza del envío
|
||||
print(f" [+] Enviado '{message}'")
|
||||
|
||||
# Cerrar la conexión
|
||||
connection.close()
|
||||
50
catch-all/05_infra_test/02_rabbitmq/02work-queues/worker.py
Normal file
50
catch-all/05_infra_test/02_rabbitmq/02work-queues/worker.py
Normal file
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python
|
||||
import pika
|
||||
import sys
|
||||
import os
|
||||
import time
|
||||
|
||||
|
||||
def main():
|
||||
# Establecer la conexión con el servidor RabbitMQ
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters('localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
# Comprobar si la cola existe
|
||||
try:
|
||||
channel.queue_declare(queue='task_queue', durable=True)
|
||||
except pika.exceptions.ChannelClosedByBroker:
|
||||
print(' [!] Error al crear la cola. ¿Está el servidor RabbitMQ corriendo?')
|
||||
sys.exit(1)
|
||||
except e:
|
||||
print(f' [!] Error: {e}')
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
# Consumir mensajes
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(f"[+] Recibido {body.decode()}")
|
||||
time.sleep(body.count(b'.'))
|
||||
print("[i] Hecho")
|
||||
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||
|
||||
# Consumir mensajes de la cola 'task_queue'
|
||||
channel.basic_qos(prefetch_count=1)
|
||||
channel.basic_consume(queue='task_queue', on_message_callback=callback)
|
||||
|
||||
# Iniciar la escucha
|
||||
print('[i] Esperando mensajes. Para salir presiona CTRL+C')
|
||||
channel.start_consuming()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except KeyboardInterrupt:
|
||||
print(' [!] Saliendo')
|
||||
try:
|
||||
sys.exit(0)
|
||||
except SystemExit:
|
||||
os._exit(0)
|
||||
Reference in New Issue
Block a user