Skip to content

Instantly share code, notes, and snippets.

@grasuth
Created September 12, 2011 16:39
Show Gist options
  • Select an option

  • Save grasuth/1211719 to your computer and use it in GitHub Desktop.

Select an option

Save grasuth/1211719 to your computer and use it in GitHub Desktop.
simple kombu send/receive example (1000 messages)
from __future__ import with_statement
from optparse import OptionParser
from kombu import BrokerConnection
def process_args():
parser = OptionParser()
parser.add_option("-l", "--listen", dest="listen",
action="store_true", help="listen and print messages", default=False)
(options, args) = parser.parse_args()
options.message = " ".join(args)
return options
def main():
options = process_args()
if options.listen:
with BrokerConnection("amqp://guest:guest@localhost:5672//") as connection:
#: Create consumer using our callback and queue.
#: Second argument can also be a list to consume from
#: any number of queues.
with connection.SimpleQueue("my_queue") as queue:
#: This waits for a single event. Note that this event may not
#: be a message, or a message that is to be delivered to the consumers
#: channel, but any event received on the connection.
while True:
message = queue.get(block=True)
message.ack()
print(message.payload)
else:
# send message
with BrokerConnection("amqp://guest:guest@localhost:5672//") as conn:
with conn.SimpleQueue("my_queue") as queue:
for i in range(1000):
queue.put({"message": options.message, "count" : i}, serializer="json", compression="zlib")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment