Skip to content

Instantly share code, notes, and snippets.

@rakrup
Last active December 16, 2015 15:29
Show Gist options
  • Save rakrup/5456133 to your computer and use it in GitHub Desktop.
Save rakrup/5456133 to your computer and use it in GitHub Desktop.
This is program analogous to any hello-world program we write in any other language. Send_Msg.py This python program works like a Producer. It sends a message on to RabbitMQ.It connects to the default Exchange and creates a queue called and then sends a message to the queue. Recieve_Msg,py This program shall consume the messages from the Hello_W…
#!/usr/bin/env python
import pika
import time
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='Hello_World', 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('.') )
print " [x] Done"
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback,
queue='Hello_World')
channel.start_consuming()
#!/usr/bin/env python
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='Hello_World', durable=True)
message = ' '.join(sys.argv[1:]) or "Hello World!"
channel.basic_publish(exchange='',
routing_key='Hello_World',
body=message,
properties=pika.BasicProperties(
delivery_mode = 2, # make message persistent
))
print " [x] Sent %r" % (message,)
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment