add messagequeue examples
This commit is contained in:
		
							
								
								
									
										20
									
								
								messaqeue/new_task.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								messaqeue/new_task.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
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=2,  # make message persistent
 | 
			
		||||
    ))
 | 
			
		||||
print(" [x] Sent %r" % message)
 | 
			
		||||
connection.close()
 | 
			
		||||
							
								
								
									
										19
									
								
								messaqeue/recieve.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								messaqeue/recieve.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
import pika
 | 
			
		||||
 | 
			
		||||
connection = pika.BlockingConnection(
 | 
			
		||||
    pika.ConnectionParameters(host='localhost'))
 | 
			
		||||
channel = connection.channel()
 | 
			
		||||
 | 
			
		||||
channel.queue_declare(queue='hello')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def callback(ch, method, properties, body):
 | 
			
		||||
    print(" [x] Received %r" % 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()
 | 
			
		||||
							
								
								
									
										14
									
								
								messaqeue/send.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								messaqeue/send.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
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()
 | 
			
		||||
							
								
								
									
										23
									
								
								messaqeue/worker.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								messaqeue/worker.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
#!/usr/bin/env python
 | 
			
		||||
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(" [x] Received %r" % body)
 | 
			
		||||
    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