python redis队列_python实现简单的redis 队列 SIMPLE PYTHON QUEUE WITH REDIS

The following article shows how to useredisto build a simple multi-producer, multi-consumer Queue with an interface similar to thepython

standardlib queue. With this queue you can easily share data between multiple processes or offload time consumig calculations to multiple worker processes.

To store the data we use theredis list data type. Redis Lists stores simple strings sorted by insertion order.

The following redis commands are used:

The implementation uses theredis-pylibrary to talk to the server.

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)

使用方法:

>>> from RedisQueue import RedisQueue

>>> q = RedisQueue('test')

>>> q.put('hello world')

Now if we have a look at the redis database with theredis-cliclient

it shows the expected results:

redis 127.0.0.1:6379> keys *

1) "queue:test"

redis 127.0.0.1:6379> type queue:test

list

redis 127.0.0.1:6379> llen queue:test

(integer) 1

redis 127.0.0.1:6379> lrange queue:test 0 1

1) "hello world"

We can get the item from a different script with:

>>> from RedisQueue import RedisQueue

>>> q = RedisQueue('test')

>>> q.get()

'hello world'

A subsequent call ofq.get()will block until anotherone puts a new item into the Queue.

The next step would be to an endoder/decoder (e.gpython-json) to the Queue so that you are not limited to send strings.

There alredy exists the nice and simplehotqueuelibrary which has the same interface as the above example and provides encoding/decoding.

Other mentionable queue implementations with a redis backend are:

FRom:http://peter-hoffmann.com/2012/python-simple-queue-redis-queue.html

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-09-18 23:44

浏览 1137

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值