Skip to content

Instantly share code, notes, and snippets.

@brainstorm
Created June 26, 2013 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainstorm/5866087 to your computer and use it in GitHub Desktop.
Save brainstorm/5866087 to your computer and use it in GitHub Desktop.
Simple AMQP tester
#!/usr/bin/env python
from kombu import BrokerConnection, Exchange, Queue
media_exchange = Exchange("media", "direct", durable=True)
video_queue = Queue("video", exchange=media_exchange, routing_key="video")
def process_media(body, message):
print body
message.ack()
# connections
with BrokerConnection("amqp://galaxy:3rSf4OyXqz@galerina.biotech.kth.se/") as conn:
# Declare the video queue so that the messages can be delivered.
# It is a best practice in Kombu to have both publishers and
# consumers declare the queue.
video_queue(conn.channel()).declare()
# produce
with conn.Producer(exchange=media_exchange,
serializer="json", routing_key="video") as producer:
producer.publish({"name": "/tmp/lolcat1.avi", "size": 1301013})
# consume
with conn.Consumer(video_queue, callbacks=[process_media]) as consumer:
# Process messages and handle events on all channels
while True:
conn.drain_events()
# Consume from several queues on the same channel:
video_queue = Queue("video", exchange=media_exchange, key="video")
image_queue = Queue("image", exchange=media_exchange, key="image")
with connection.Consumer([video_queue, image_queue],
callbacks=[process_media]) as consumer:
while True:
connection.drain_events()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment