RQ ( Redis Queue ) 是一个简单的 Python 库,用于实现作业的队列,并在后台进行处理。后端基于 Redis ,可方便集成到 Web 应用中,要求 Redis >= 2.6.0
示例代码:
from rq import Queue from redis import Redis from somewhere import count_words_at_url # Tell RQ what Redis connection to use redis_conn = Redis() q = Queue(connection=redis_conn) # no args implies the default queue # Delay execution of count_words_at_url('http://nvie.com') job = q.enqueue(count_words_at_url, 'http://nvie.com') print job.result # => None # Now, wait a while, until the worker is finished time.sleep(2) print job.result # => 889
后台作业处理 worker:
$ rqworker *** Listening for work on default Got count_words_at_url('http://nvie.com') from default Job result = 818 *** Listening for work on default