Skip to content

Instantly share code, notes, and snippets.

@dbrandt
Created February 13, 2015 13:59
Show Gist options
  • Save dbrandt/76acf17454914016588b to your computer and use it in GitHub Desktop.
Save dbrandt/76acf17454914016588b to your computer and use it in GitHub Desktop.
RabbitMQ ping
import rabbitpy
def send():
with rabbitpy.Connection() as conn:
with conn.channel() as chan:
ex = rabbitpy.DirectExchange(chan, "ping_ex")
ex.declare()
while True:
m = rabbitpy.Message(chan, "ping")
m.publish(ex, "ping_ex")
yield
def collect():
with rabbitpy.Connection() as conn:
with conn.channel() as chan:
ex = rabbitpy.DirectExchange(chan, "ping_ex")
ex.declare()
q = rabbitpy.Queue(chan)
q.declare()
q.bind(ex, "ping_ex")
for m in q:
m.ack()
yield m.body
if __name__ == "__main__":
import sys
import time
if len(sys.argv) == 2 and sys.argv[1] == "send":
for x in send():
time.sleep(1)
else:
for x in collect():
print(x.decode("utf8"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment