Change semantics of work(). Add work_burst().

work() will now start the worker run loop, and work_burst() now leads to
the burst-then-quit behaviour.
main
Vincent Driessen 13 years ago
parent d761dd0280
commit d780c929c0

@ -51,7 +51,7 @@ with the following content:
from rq import Queue, Worker from rq import Queue, Worker
q = Queue() q = Queue()
Worker(q).work_forever() Worker(q).work()
After that, start a worker instance: After that, start a worker instance:
@ -66,12 +66,12 @@ them:
from rq import Queue, Worker from rq import Queue, Worker
queues = map(Queue, ['high', 'normal', 'low']) queues = map(Queue, ['high', 'normal', 'low'])
Worker(queues).work() Worker(queues).work_burst()
Which will keep popping jobs from the given queues, giving precedence to the Which will keep popping jobs from the given queues, giving precedence to the
`high` queue, then `normal`, etc. It will return when there are no more jobs `high` queue, then `normal`, etc. It will return when there are no more jobs
left (contrast this to the previous example using `Worker.work_forever()`, left (contrast this to the previous example using `Worker.work()`, which will
which will never return since it keeps waiting for new work to arrive). never return since it keeps waiting for new work to arrive).
# Installation # Installation

@ -7,4 +7,4 @@ conn.push(Redis())
if __name__ == '__main__': if __name__ == '__main__':
q = Queue() q = Queue()
Worker(q).work_forever() Worker(q).work()

@ -68,10 +68,10 @@ class Worker(object):
self.fork_and_perform_job(job) self.fork_and_perform_job(job)
return did_work return did_work
def work_forever(self): def work(self):
self._work(False) self._work(False)
def work(self): def work_burst(self):
return self._work(True) return self._work(True)
def fork_and_perform_job(self, job): def fork_and_perform_job(self, job):

@ -143,10 +143,10 @@ class TestWorker(RQTestCase):
"""Worker processes work, then quits.""" """Worker processes work, then quits."""
fooq, barq = Queue('foo'), Queue('bar') fooq, barq = Queue('foo'), Queue('bar')
w = Worker([fooq, barq]) w = Worker([fooq, barq])
self.assertEquals(w.work(), False, 'Did not expect any work on the queue.') self.assertEquals(w.work_burst(), False, 'Did not expect any work on the queue.')
fooq.enqueue(testjob, name='Frank') fooq.enqueue(testjob, name='Frank')
self.assertEquals(w.work(), True, 'Expected at least some work done.') self.assertEquals(w.work_burst(), True, 'Expected at least some work done.')
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save