-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_queue.py
50 lines (38 loc) · 1.37 KB
/
redis_queue.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
# -*- coding: utf-8 -*-
import redis
class RedisQueue(object):
"""Simple Queue with Redis Backend"""
def __init__(self, name, namespace='queue', **redis_kwargs):
"""The default connection parameters are: host='localhost', port=6379, db=0"""
self.db = redis.Redis(**redis_kwargs)
self.key = '%s:%s' %(namespace, name)
def qsize(self):
"""Return the approximate size of the queue."""
return self.db.llen(self.key)
def empty(self):
"""Return True if the queue is empty, False otherwise."""
return self.qsize() == 0
def put(self, item):
"""Put item into the queue."""
self.db.rpush(self.key, item)
def get(self, 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."""
if block:
item = self.db.blpop(self.key, timeout=timeout)
if item:
item = item[1]
else:
item = self.db.lpop(self.key)
return item
def get_nowait(self):
"""Equivalent to get(False)."""
return self.get(False)
def __iter__(self):
return self
def next(self):
item = self.get(False)
if item is None:
raise StopIteration
return item