2011-09-11 14:05:08 +00:00
|
|
|
.. _guide-simple:
|
|
|
|
|
2010-08-04 13:20:12 +00:00
|
|
|
==================
|
|
|
|
Simple Interface
|
|
|
|
==================
|
|
|
|
|
|
|
|
.. contents::
|
|
|
|
:local:
|
|
|
|
|
|
|
|
|
|
|
|
:mod:`kombu.simple` is a simple interface to AMQP queueing.
|
|
|
|
It is only slightly different from the :class:`~Queue.Queue` class in the
|
|
|
|
Python Standard Library, which makes it excellent for users with basic
|
|
|
|
messaging needs.
|
|
|
|
|
2010-08-04 13:36:00 +00:00
|
|
|
Instead of defining exchanges and queues, the simple classes only requires
|
|
|
|
two arguments, a connection channel and a name. The name is used as the
|
|
|
|
queue, exchange and routing key. If the need arises, you can specify
|
2012-11-21 16:24:49 +00:00
|
|
|
a :class:`~kombu.Queue` as the name argument instead.
|
2010-08-04 13:20:12 +00:00
|
|
|
|
2012-10-23 15:20:32 +00:00
|
|
|
In addition, the :class:`~kombu.Connection` comes with
|
2010-08-04 13:20:12 +00:00
|
|
|
shortcuts to create simple queues using the current connection::
|
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
>>> queue = connection.SimpleQueue('myqueue')
|
2010-08-04 13:20:12 +00:00
|
|
|
>>> # ... do something with queue
|
|
|
|
>>> queue.close()
|
|
|
|
|
|
|
|
|
|
|
|
This is equivalent to::
|
|
|
|
|
|
|
|
>>> from kombu import SimpleQueue, SimpleBuffer
|
|
|
|
|
|
|
|
>>> channel = connection.channel()
|
|
|
|
>>> queue = SimpleBuffer(channel)
|
|
|
|
>>> # ... do something with queue
|
|
|
|
>>> channel.close()
|
2010-08-04 13:36:00 +00:00
|
|
|
>>> queue.close()
|
2010-08-04 13:20:12 +00:00
|
|
|
|
2011-09-11 14:05:08 +00:00
|
|
|
.. _simple-send-receive:
|
2010-08-04 13:20:12 +00:00
|
|
|
|
|
|
|
Sending and receiving messages
|
|
|
|
==============================
|
|
|
|
|
|
|
|
The simple interface defines two classes; :class:`~kombu.simple.SimpleQueue`,
|
|
|
|
and :class:`~kombu.simple.SimpleBuffer`. The former is used for persistent
|
|
|
|
messages, and the latter is used for transient, buffer-like queues.
|
|
|
|
They both have the same interface, so you can use them interchangeably.
|
|
|
|
|
|
|
|
Here is an example using the :class:`~kombu.simple.SimpleQueue` class
|
|
|
|
to produce and consume logging messages:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2011-09-11 14:05:08 +00:00
|
|
|
from __future__ import with_statement
|
2012-12-06 10:29:01 +00:00
|
|
|
|
|
|
|
import socket
|
|
|
|
import datetime
|
2010-08-04 13:20:12 +00:00
|
|
|
from time import time
|
2012-06-24 15:32:17 +00:00
|
|
|
from kombu import Connection
|
2012-12-06 10:29:01 +00:00
|
|
|
|
|
|
|
|
2010-08-04 13:20:12 +00:00
|
|
|
class Logger(object):
|
2012-12-06 10:29:01 +00:00
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
def __init__(self, connection, queue_name='log_queue',
|
|
|
|
serializer='json', compression=None):
|
2012-12-06 10:29:01 +00:00
|
|
|
self.queue = connection.SimpleQueue(queue_name)
|
2010-08-04 13:20:12 +00:00
|
|
|
self.serializer = serializer
|
|
|
|
self.compression = compression
|
2012-12-06 10:29:01 +00:00
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
def log(self, message, level='INFO', context={}):
|
|
|
|
self.queue.put({'message': message,
|
|
|
|
'level': level,
|
|
|
|
'context': context,
|
|
|
|
'hostname': socket.gethostname(),
|
|
|
|
'timestamp': time()},
|
2010-08-04 13:20:12 +00:00
|
|
|
serializer=self.serializer,
|
|
|
|
compression=self.compression)
|
2012-12-06 10:29:01 +00:00
|
|
|
|
2010-08-04 13:20:12 +00:00
|
|
|
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
|
2012-12-06 10:29:01 +00:00
|
|
|
|
2010-08-04 13:20:12 +00:00
|
|
|
def close(self):
|
|
|
|
self.queue.close()
|
2012-12-06 10:29:01 +00:00
|
|
|
|
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
if __name__ == '__main__':
|
2011-09-11 14:05:08 +00:00
|
|
|
from contextlib import closing
|
2012-12-06 10:29:01 +00:00
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
with Connection('amqp://guest:guest@localhost:5672//') as conn:
|
2012-12-06 10:29:01 +00:00
|
|
|
with closing(Logger(conn)) as logger:
|
|
|
|
|
2011-09-11 14:05:08 +00:00
|
|
|
# Send message
|
2012-06-24 15:32:17 +00:00
|
|
|
logger.log('Error happened while encoding video',
|
|
|
|
level='ERROR',
|
|
|
|
context={'filename': 'cutekitten.mpg'})
|
2012-12-06 10:29:01 +00:00
|
|
|
|
2011-09-11 14:05:08 +00:00
|
|
|
# Consume and process message
|
2012-12-06 10:29:01 +00:00
|
|
|
|
2011-09-11 14:05:08 +00:00
|
|
|
# This is the callback called when a log message is
|
|
|
|
# received.
|
|
|
|
def dump_entry(entry):
|
2012-12-06 10:29:01 +00:00
|
|
|
date = datetime.datetime.fromtimestamp(entry['timestamp'])
|
2012-06-24 15:32:17 +00:00
|
|
|
print('[%s %s %s] %s %r' % (date,
|
|
|
|
entry['hostname'],
|
|
|
|
entry['level'],
|
|
|
|
entry['message'],
|
|
|
|
entry['context']))
|
2012-12-06 10:29:01 +00:00
|
|
|
|
2011-09-11 14:05:08 +00:00
|
|
|
# Process a single message using the callback above.
|
|
|
|
logger.process(dump_entry, n=1)
|
2012-12-06 10:29:01 +00:00
|
|
|
|