基于异步tornado-redis的可靠队列包
Torrelque的Python项目详细描述
托雷尔克
torrelque是一个python 3包,它提供了一个可靠的redis支持的队列并在其上运行 龙卷风IO回路。
不用再费劲,很容易说包是所描述队列的实现 在this blog post中 做了一些必要的改变和改进。
还有一个相关的同步队列包,名为saferedisqueue。
安装
pip install Torrelque
使用
#!/usr/bin/env python3importrandomimportloggingimporttornadoredisfromtornadoimportgen,ioloopfromtorrelqueimportTorrelquelogger=logging.getLogger(__name__)@gen.coroutinedefproduce():redis=tornadoredis.Client()queue=Torrelque(redis,ioloop.IOLoop.current())whileTrue:for_inrange(5):task={'value':random.randint(0,99)}logger.debug('Produced task %s',task)yieldqueue.enqueue(task)yieldgen.sleep(10)@gen.coroutinedefprocess(task_data):logger.debug('Consmed task %s',task_data)yieldgen.sleep(1)@gen.coroutinedefconsume():redis=tornadoredis.Client()queue=Torrelque(redis,ioloop.IOLoop.current())whileTrue:task_id,task_data=yieldqueue.dequeue()ifnottask_id:continuetry:yieldprocess(task_data)yieldqueue.release(task_id)exceptException:logger.exception('Job processing has failed')yieldqueue.requeue(task_id,delay=30)@gen.coroutinedefmain():for_inrange(4):ioloop.IOLoop.current().spawn_callback(consume)yieldproduce()if__name__=='__main__':logging.basicConfig(level=logging.DEBUG,format='%(asctime)s%(message)s')ioloop.IOLoop.instance().run_sync(main)