Skip to content

Instantly share code, notes, and snippets.

@squaremo
Created February 25, 2014 13:27
Show Gist options
  • Save squaremo/9208682 to your computer and use it in GitHub Desktop.
Save squaremo/9208682 to your computer and use it in GitHub Desktop.
RabbitMQ primer RPC
import pika
import sys
import logging
logging.basicConfig(level=logging.CRITICAL)
params = pika.URLParameters('amqp://guest:guest@localhost:5672/%2F')
conn = pika.BlockingConnection(params)
ch = conn.channel()
ch.queue_declare('taskrabbit')
ok = ch.queue_declare(exclusive=True)
mailbox = ok.method.queue
ch.basic_publish('', 'taskrabbit', sys.argv[1],
properties=pika.BasicProperties(reply_to=mailbox))
try:
for (f, h, b) in ch.consume(mailbox):
print b
ch.basic_ack(f.delivery_tag)
break
except KeyboardInterrupt:
pass
conn.close()
import pika
import sys
import logging
logging.basicConfig(level=logging.CRITICAL)
params = pika.URLParameters('amqp://guest:guest@localhost:5672/%2F')
conn = pika.BlockingConnection(params)
ch = conn.channel()
ch.queue_declare('taskrabbit')
try:
for (f, h, req) in ch.consume(queue='taskrabbit'):
replyto = h.reply_to
reply = req.upper()
print("Got '%s', responding '%s'", (req, reply))
ch.basic_publish('', replyto, reply)
ch.basic_ack(f.delivery_tag)
except KeyboardInterrupt:
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment