summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorHVN <hvnsweeting@gmail.com>2012-12-06 17:29:01 +0700
committerHVN <hvnsweeting@gmail.com>2012-12-06 17:29:01 +0700
commite1c2bcec7e41247aa4bf3bd35eea571777665fc4 (patch)
tree8de8de1aaf514cbdaf2368482904e3faf9be6467 /docs
parentbdc1e51f67f3c9ad74a72bcc061ffe4f1c56b0dc (diff)
downloadkombu-e1c2bcec7e41247aa4bf3bd35eea571777665fc4.tar.gz
code tested with python 2.7.3 on Ubuntu11.04 64bit
Diffstat (limited to 'docs')
-rw-r--r--docs/userguide/simple.rst40
1 files changed, 20 insertions, 20 deletions
diff --git a/docs/userguide/simple.rst b/docs/userguide/simple.rst
index 69caaf2d..e11e0ccb 100644
--- a/docs/userguide/simple.rst
+++ b/docs/userguide/simple.rst
@@ -50,23 +50,22 @@ Here is an example using the :class:`~kombu.simple.SimpleQueue` class
to produce and consume logging messages:
.. code-block:: python
-
from __future__ import with_statement
-
- from socket import gethostname
+
+ import socket
+ import datetime
from time import time
-
from kombu import Connection
-
-
+
+
class Logger(object):
-
+
def __init__(self, connection, queue_name='log_queue',
serializer='json', compression=None):
- self.queue = connection.SimpleQueue(self.queue_name)
+ self.queue = connection.SimpleQueue(queue_name)
self.serializer = serializer
self.compression = compression
-
+
def log(self, message, level='INFO', context={}):
self.queue.put({'message': message,
'level': level,
@@ -75,40 +74,41 @@ to produce and consume logging messages:
'timestamp': time()},
serializer=self.serializer,
compression=self.compression)
-
+
def process(self, callback, n=1, timeout=1):
for i in xrange(n):
log_message = self.queue.get(block=True, timeout=1)
entry = log_message.payload # deserialized data.
callback(entry)
log_message.ack() # remove message from queue
-
+
def close(self):
self.queue.close()
-
-
+
+
if __name__ == '__main__':
from contextlib import closing
-
+
with Connection('amqp://guest:guest@localhost:5672//') as conn:
- with closing(Logger(connection)) as logger:
-
+ with closing(Logger(conn)) as logger:
+
# Send message
logger.log('Error happened while encoding video',
level='ERROR',
context={'filename': 'cutekitten.mpg'})
-
+
# Consume and process message
-
+
# This is the callback called when a log message is
# received.
def dump_entry(entry):
- date = datetime.fromtimestamp(entry['timestamp'])
+ date = datetime.datetime.fromtimestamp(entry['timestamp'])
print('[%s %s %s] %s %r' % (date,
entry['hostname'],
entry['level'],
entry['message'],
entry['context']))
-
+
# Process a single message using the callback above.
logger.process(dump_entry, n=1)
+