refactor(rabbitmq): send tasks from one queue to some workers
This commit is contained in:
		
							
								
								
									
										23
									
								
								rabbitmq/send_message.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								rabbitmq/send_message.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
import sys
 | 
			
		||||
 | 
			
		||||
import pika
 | 
			
		||||
 | 
			
		||||
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()
 | 
			
		||||
							
								
								
									
										24
									
								
								rabbitmq/worker.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								rabbitmq/worker.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
import pika
 | 
			
		||||
 | 
			
		||||
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()
 | 
			
		||||
		Reference in New Issue
	
	Block a user