Support quitting when all work is done (i.e. queue is empty).

main
Vincent Driessen 13 years ago
parent 8dc2b15a6e
commit a77c3d9104

@ -4,7 +4,7 @@ except ImportError:
from logging import Logger
from .worker import Worker
def run_daemon(queue_keys, rv_ttl=500):
def run_daemon(queue_keys, rv_ttl=500, quit_when_done=False):
"""Simple implementation of a Redis queue worker, based on
http://flask.pocoo.org/snippets/73/
@ -17,4 +17,4 @@ def run_daemon(queue_keys, rv_ttl=500):
for key in queue_keys:
log.info('- %s' % (key,))
worker.work()
worker.work(quit_when_done)

@ -45,10 +45,16 @@ class Worker(object):
self.log.debug(message)
procname.setprocname('rq: %s' % (message,))
def work(self):
def work(self, quit_when_done=False):
while True:
self.procline('Waiting on %s' % (', '.join(self.queue_names()),))
queue, msg = conn.blpop(self.queue_keys())
if quit_when_done:
value = conn.lpop(self.queue_keys())
if value is None:
break # No more work, so quitting
queue, msg = value
else:
queue, msg = conn.blpop(self.queue_keys())
self.fork_and_perform_job(queue, msg)
def fork_and_perform_job(self, queue, msg):

Loading…
Cancel
Save