summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAsk Solem <askh@opera.com>2010-11-24 12:31:20 +0100
committerAsk Solem <askh@opera.com>2010-11-24 12:31:20 +0100
commitfe16e2b9650f09248e5e82cde2385b52f305bc08 (patch)
treed14f12fe7405416f48efb17469c5697efc41b497 /examples
parent29dd0e37b6b7cfbeabbcef64601a7cd2e5240062 (diff)
downloadkombu-fe16e2b9650f09248e5e82cde2385b52f305bc08.tar.gz
Added examples/ directory with demo_send and demo_receive
Diffstat (limited to 'examples')
-rw-r--r--examples/demo_receive.py46
-rw-r--r--examples/demo_send.py35
2 files changed, 81 insertions, 0 deletions
diff --git a/examples/demo_receive.py b/examples/demo_receive.py
new file mode 100644
index 00000000..e6ca60c9
--- /dev/null
+++ b/examples/demo_receive.py
@@ -0,0 +1,46 @@
+"""
+Example of simple consumer that waits for a single message, acknowledges it
+and exits.
+"""
+
+from kombu import BrokerConnection, Exchange, Queue, Consumer
+from pprint import pformat
+
+#: By default messages sent to exchanges are persistent (delivery_mode=2),
+#: and queues and exchanges are durable.
+exchange = Exchange("kombu_demo", type="direct")
+queue = Queue("kombu_demo", exchange, routing_key="kombu_demo")
+
+
+def pretty(obj):
+ return pformat(obj, indent=4)
+
+#: This is the callback applied when a message is received.
+def handle_message(body, message):
+ print("Received message: %r" % (body, ))
+ print(" properties:\n%s" % (pretty(message.properties), ))
+ print(" delivery_info:\n%s" % (pretty(message.delivery_info), ))
+ message.ack()
+
+#: Create a connection and a channel.
+#: If hostname, userid, password and virtual_host is not specified
+#: the values below are the default, but listed here so it can
+#: be easily changed.
+connection = BrokerConnection(hostname="localhost",
+ userid="guest",
+ password="guest",
+ virtual_host="/")
+channel = connection.channel()
+
+#: Create consumer using our callback and queue.
+#: Second argument can also be a list to consume from
+#: any number of queues.
+consumer = Consumer(channel, queue, callbacks=[handle_message])
+consumer.consume()
+
+#: 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.
+connection.drain_events()
+
+
diff --git a/examples/demo_send.py b/examples/demo_send.py
new file mode 100644
index 00000000..11790590
--- /dev/null
+++ b/examples/demo_send.py
@@ -0,0 +1,35 @@
+"""
+
+Example producer that sends a single message and exits.
+
+You can use `demo_receive.py` to receive the message sent.
+
+"""
+
+from kombu import BrokerConnection, Exchange, Queue, Producer
+
+#: By default messages sent to exchanges are persistent (delivery_mode=2),
+#: and queues and exchanges are durable.
+exchange = Exchange("kombu_demo", type="direct")
+queue = Queue("kombu_demo", exchange, routing_key="kombu_demo")
+
+
+#: Create connection and channel.
+#: If hostname, userid, password and virtual_host is not specified
+#: the values below are the default, but listed here so it can
+#: be easily changed.
+connection = BrokerConnection(hostname="localhost",
+ userid="guest",
+ password="guest",
+ virtual_host="/")
+channel = connection.channel()
+
+#: Producers are used to publish messages.
+#: Routing keys can also be specifed as an argument to `publish`.
+producer = Producer(channel, exchange, routing_key="kombu_demo")
+
+#: Publish the message using the json serializer (which is the default),
+#: and zlib compression. The kombu consumer will automatically detect
+#: encoding, serializiation and compression used and decode accordingly.
+producer.publish({"hello": "world"}, serializer="json",
+ compression="zlib")