使用mongo作为后端存储的队列。
mongoqueue的Python项目详细描述
属性
Isolation
Do not let different consumers process the same message.
Reliablity
Do not let a failed consumer disappear an item.
Atomic
Operations on the queue are atomic.
用法
队列可以用mongo集合和使用者实例化 标识符。使用者标识符有助于区分多个队列 正在从队列中获取作业的消费者:
>> from pymongo import Connection >> from mongoqueue import MongoQueue >> queue = MongoQueue( ... Connection(TEST_DB).doctest_queue, ... consumer_id="consumer-1", ... timeout=300, ... max_attempts=3)
MongoQueue类timeout参数指定 seconds a一个作业在其 被认为是失败的。
超时或错误超过^{tt3}的作业$ 参数被认为是永久失败的,并且将不再是 处理。
可以通过传递字典将新作业/项放入队列:
>> queue.put({"foobar": 1})
作业priority键和整数值可以在 将导致作业在下一步之前处理的字典 优先项目:
>> queue.put({"foobar": 0}, priority=1})
通过调用队列上的next方法可以取出项。 这将返回作业对象:
>> job = queue.next() >> job.payload {"foobar": 1}
job类公开作业上的一些控制方法,用于标记进度, 完成、错误或将作业释放回队列中。
- ^{tt6}$ Marks a job as complete and removes it from the queue.
- ^{tt7}$ Optionally specified with a message, releases the job back to the
- queue, and increments its attempts, and stores the error message on the job.
- ^{tt8}$ Optionally takes a progress count integer, notes progress on the job
- and resets the lock timeout.
- ^{tt9}$ Release a job back to the pool. The attempts counter is not modified.
为方便起见,该作业支持上下文管理器协议:
>> with job as data: ... print data['payload'] {"foobar: 0}
如果上下文关闭被退出而没有作业被标记为完整的, 如果有异常,错误将存储在作业中。
灵感来源于
- [0]https://github.com/skiz/mongo_queue/blob/master/lib/mongo_queue.rb
- [1]http://blog.boxedice.com/2011/09/28/replacing-rabbitmq-with-mongodb/
- [2]http://blog.boxedice.com/2011/04/13/queueing-mongodb-using-mongodb/
- [3]https://github.com/lunaru/mongoqueue
- [4]http://www.captaincodeman.com/2011/05/28/simple-service-bus-message-queue-mongodb/
运行测试
单元测试可以使用
$ python setup.py nosetests
更改
0.6.0-2013年2月4日-将传入的数据与作业中的元数据隔离。 0.5.2-2012年12月9日-修复来自Pymongo 2.4的排序参数中的回归 0.5.1-2012年12月2日-Readme数据文件的打包修复程序。
学分
Kapil Thangavelu,作者和维护者 Dustin Laurence,Pymongo 2.4的排序修复 Jonathan Sackett,工作数据隔离。