-
Notifications
You must be signed in to change notification settings - Fork 0
/
redisqueue.py
50 lines (39 loc) · 1.73 KB
/
redisqueue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class RedisQueue(object):
"""Simple Queue with Redis Backend"""
def __init__(self, redis, name='default', namespace='queue',
serializer='json'):
self.__db = redis
self.namespace = namespace
self.name = name
if serializer == 'pickle':
import pickle as serializer
else:
import ujson as serializer
self.serializer = serializer
def gen_key(self, name=None, namespace=None):
return '%s:%s' % (namespace or self.namespace, name or self.name)
def qsize(self, name=None, namespace=None):
"""Return the approximate size of the queue."""
return self.__db.llen(self.gen_key(name, namespace))
def empty(self, name=None, namespace=None):
"""Return True if the queue is empty, False otherwise."""
return self.qsize(name) == 0
def put(self, item, name=None, namespace=None):
"""Put item into the queue."""
return self.__db.rpush(self.gen_key(name, namespace),
self.serializer.dumps(item))
def get(self, name=None, namespace=None, block=True, timeout=None):
""" Remove and return an item from the queue.
If optional args block is true and timeout is None (the default), block
if necessary until an item is available."""
key = self.gen_key(name, namespace)
if block:
item = self.__db.blpop(key, timeout=timeout)
else:
item = self.__db.lpop(key)
if item:
item = self.serializer.loads(item[1])
return item
def get_nowait(self, name=None, namespace=None):
"""Equivalent to get(block=False)."""
return self.get(name=name, namespace=namespace, block=False)