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
andloads
, pickle is the default, useNone
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 commonlydbfilename
.
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 toTrue
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: Returns: The deserialized object from the queue.
Return type: 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 toTrue
if not given)