Skip to content

Commit

Permalink
Merge pull request #462 from conslo/worker-hash
Browse files Browse the repository at this point in the history
Worker hash
  • Loading branch information
nvie committed Dec 14, 2014
2 parents e1801c5 + 6ef9177 commit cd0c3c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions rq/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,16 @@ def pop_exc_handler(self):
"""Pops the latest exception handler off of the exc handler stack."""
return self._exc_handlers.pop()

def __eq__(self, other):
"""Equality does not take the database/connection into account"""
if not isinstance(other, self.__class__):
raise TypeError('Cannot compare workers to other types (of workers)')
return self.name == other.name

def __hash__(self):
"""The hash does not take the database/connection into account"""
return hash(self.name)


class SimpleWorker(Worker):
def _install_signal_handlers(self, *args, **kwargs):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,12 @@ def test_work_unicode_friendly(self):
'Expected at least some work done.')
self.assertEquals(job.result, 'Hi there, Adam!')
self.assertEquals(job.description, '你好 世界!')

def test_worker_hash_(self):
"""Workers are hashed by their .name attribute"""
q = Queue('foo')
w1 = Worker([q], name="worker1")
w2 = Worker([q], name="worker2")
w3 = Worker([q], name="worker1")
worker_set = set([w1, w2, w3])
self.assertEquals(len(worker_set), 2)

0 comments on commit cd0c3c9

Please sign in to comment.