带有redis的优先级消息队列
pyrediq的Python项目详细描述
除虫菊
pyridiq(py re ddi ck)是一个python(2.7)库,提供 使用Redis实现优先级消息队列。信息 负载由MessagePack序列化。
pyrediq是根据MIT License (MIT)授权的。
基本用法
importredisfrompyrediqimportPriorityQueue,QueueEmptyredis_conn=redis.StrictRedis()# create a queuequeue=PriorityQueue('myqueue',redis_conn)# enqueue a message to the queuequeue.put({'mykey':'myvalue'},priority=-1)# create a consumerwithqueue.consumer()asconsumer:try:msg=consumer.get(block=False)exceptQueueEmpty:raisesuccess=do_task(msg.payload)# the message is either acked or rejected after task is done.# a rejected message can optionally be requeuedifsuccess:consumer.ack(msg)else:consumer.reject(msg,requeue=True)queue.close()
要从队列中删除所有消息并删除队列本身,请运行 priorityqueue.purge方法:
queue.purge()
安装
要使用pip安装pyrediq,请在shell上运行此命令:
pip install pyrediq
要使用pyrediq,需要在 电脑。例如,在一个debian框上,运行这个命令应该 足够:
sudo apt-get install redis-server
有关安装,请参考official Redis distribution site 程序。