基于redis的队列、堆栈和优先级队列
fastrq的Python项目详细描述
fastrq-在redis上构建的队列、堆栈和优先级队列
功能
- 抽象队列、deque、capped queue/deque和可溢出capped queue/deque
- 抽象堆栈,封顶堆栈
- 抽象优先级队列、上限优先级队列和可溢出上限优先级队列
- 推送和弹出支持批量操作
- 使用lua脚本节省rtt(往返时间)
- 支持获取成员索引
- 仅当成员不在队列中时才支持推送
- 仅当队列已经存在/不存在时支持推送< /LI>
- 所有操作都是
atomic
要求
- redis=3.0.2
- python 2.7或>;=3.4
安装
通过PIP
pip install fastrq
或来源
python setup.py install
用法
fromfastrq.queueimportQueue,CappedQueuefromfastrq.dequeimportDequefromfastrq.stackimportStackfromfastrq.priorityqueueimportPriorityQueue# queueq=Queue("fastrq_queue")q.push(1)q.push([2,3])q.push_ni(1)# got [3, False]. `ni` stands `not inside`q.push_ae(1)# got 4. `ae` stands `already exists`q.push_ne(1)# got False. `ne` stands `not already exist`q.ttl(10)# set the lifetime in secondsq.range(0,-1)# got ['1', '2', '3']q.range(0,1)# got ['1', '2']q.indexof_one(1);# got 0q.indexof_one(2);# got 1q.indexof_one(4);# got Noneq.indexof_many([1,2,4]);# got {1: 0, 2: 1, 4: None}# push only if the member not inside the queueq.push_ni(4)# got [4, True]q.pop()q.pop(2)q.destruct()# destruct the queuecq=CappedQueue("fastrq_capped_queue",3)cq.push(1)cq.push(2)cq.push([3,4])# got "err_qof"cq.push(3)cq.push(4)# got "err_qf"of_cq=OfCappedQueue("fastrq_of_capped_queue",3)of_cq.push(1)of_cq.push([2,3,4])# "1" would be forced out# dequedq=Deque("fastrq_deque")dq.push_front([1,2])dq.push_back([3,4])dq.pop_front()dq.pop_back()dq.push_front_ni(3)dq.push_back_ni(5)# priority queuepq=PriorityQueue("fastrq_priority_queue")pq.push({'alibaba':1})pq.push({'google':0,'microsoft':2})pq.indexof_one('google');# got 0pq.indexof_one('alibaba');# got 1pq.indexof_one('baidu');# got Nonepq.pop()pq.pop(2)pq.push_ni('ibm',4)pq.push_ni('amazon',5)# stacks=Stack("fastrq_stack")s.push([1,2,3])s.indexof_one(1);# got 2s.indexof_one(2);# got 1s.indexof_one(3);# got 0s.pop()s.push_ni(4)
数据类型
排队
- 先进先出
- 无限容量
- 支持批量推送和批量弹出
德克
从具有更多功能的队列派生
- 支持向前推和向后推
- 支持前后弹出
封顶队列/deque
从具有更多功能的queue/deque派生
- 具有固定容量
- 完全按下将失败
- 推到一个位置不够的人会失败
可溢出的上限队列/deque
从具有更多功能的capped queue/deque派生
- 队列长度永远不会超过其容量
- 如果一端已满,则推到另一端会迫使另一端退出
堆栈
- 后进先出
- 无限容量
- 支持批量推送和批量弹出
封顶烟囱
从具有更多功能的堆栈派生
- 具有固定容量
- 推送到完全封顶的堆栈将失败
- 推到位置不够的有盖堆栈将失败
优先级队列
- 分数越低,优先级越高
- 无限容量
- 支持批量推送和批量弹出
上限优先级队列
从具有更多功能的优先级队列派生
- 具有固定容量
- 完全按下将失败
- 推到一个位置不够的有上限的位置将失败
可溢出的上限优先级队列
从具有更多功能的上限优先级队列派生
- 队列长度永远不会超过其容量
- 如果队列已满,则push to将强制输出最低优先级