Skip to content

Instantly share code, notes, and snippets.

@foowaa
Created July 16, 2019 08:49
Show Gist options
  • Save foowaa/275a05e935d13fb2ea427a7a7e7b5834 to your computer and use it in GitHub Desktop.
Save foowaa/275a05e935d13fb2ea427a7a7e7b5834 to your computer and use it in GitHub Desktop.
redis queue
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)
else:
item = self.__db.lpop(self.key)
if item:
item = item[1]
return item
def get_nowait(self):
"""Equivalent to get(False)."""
return self.get(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment