API Reference

class hotqueue.HotQueue(name, serializer=pickle, **kwargs)

Simple FIFO message queue stored in a Redis list.

Parameters:
  • name (str) – name of the queue
  • max_queue_length (int) – Maximum length the queue can grow to (default is None allows the queue to grow without any limits.
  • serializer (class, module, optional) – the class or module to serialize msgs with, must have methods or functions named dumps and loads, pickle is the default, use None to store messages in plain text (suitable for strings, integers, etc)
  • redis (redis.Redis, redislite.Redis, optional) – redis connection object, defaults to redislite.Redis with fallback to redis.Redis.
  • **kwargs

    Additional kwargs to pass to redislite.Redis, most commonly dbfilename.

Examples

>>> from hotqueue import HotQueue
>>> queue = HotQueue("myqueue", dbfilename="queue.rdb")
key

Key in Redis to store the queue

Returns:The name of the key containing the queue in redis.
Return type:str
clear()

Clear the queue of all messages, by deleting the Redis key.

consume(**kwargs)

A blocking generator that yields whenever a message is waiting in the queue.

Parameters:**kwargs

any arguments that get() can accept (block will default to True if not given)

Yields:object – The deserialized object from the queue.

Examples

>>> queue = HotQueue("example")
>>> for msg in queue.consume(timeout=1):
...     print(msg)
my message
another message
get(block=False, timeout=None)

Get a message from the queue.

Parameters:
  • block (bool) – whether or not to wait until a msg is available in the queue before returning; False by default
  • timeout (int) – When using block, if no msg is available for timeout in seconds, give up and return
Returns:

The deserialized object from the queue.

Return type:

object

Examples

>>> queue.get()
'my message'
>>> queue.get()
'another message'
put(*msgs)

Put one or more messages onto the queue. Example:

>>> queue.put("my message")
>>> queue.put("another message")

To put messages onto the queue in bulk, which can be significantly faster if you have a large number of messages:

>>> queue.put("my message", "another message", "third message")
worker(*args, **kwargs)

Decorator for using a function as a queue worker. Example:

>>> @queue.worker(timeout=1)
... def printer(msg):
...     print(msg)
>>> printer()
my message
another message

You can also use it without passing any keyword arguments:

>>> @queue.worker
... def printer(msg):
...     print(msg)
>>> printer()
my message
another message
Parameters:kwargs – any arguments that get() can accept (block will default to True if not given)