Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Worker's "all" and "find_by_key" methods should accept "connection" as argument #1

merged 1 commit into from

1 participant


Hi Vincent,

This is just a small patch to make Worker accept a connection argument, consistent with Queue.

I use this feature to find active workers in a django app I wrote: . Please take a look at it when you're free and let me know if you have any comments or suggestions :)

@selwin selwin merged commit 50ba003 into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 16, 2012
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 9 deletions.
  1. +12 −9 rq/
21 rq/
@@ -50,15 +50,17 @@ class Worker(object):
redis_workers_keys = 'rq:workers'
- def all(cls):
+ def all(cls, connection=None):
"""Returns an iterable of all Workers.
- conn = get_current_connection()
- reported_working = conn.smembers(cls.redis_workers_keys)
- return compact(map(cls.find_by_key, reported_working))
+ if connection is None:
+ connection = get_current_connection()
+ reported_working = connection.smembers(cls.redis_workers_keys)
+ workers = [cls.find_by_key(key, connection) for key in reported_working]
+ return compact(workers)
- def find_by_key(cls, worker_key):
+ def find_by_key(cls, worker_key, connection=None):
"""Returns a Worker instance, based on the naming conventions for
naming the internal Redis keys. Can be used to reverse-lookup Workers
by their Redis keys.
@@ -68,14 +70,15 @@ def find_by_key(cls, worker_key):
if not worker_key.startswith(prefix):
raise ValueError('Not a valid RQ worker key: %s' % (worker_key,))
- conn = get_current_connection()
- if not conn.exists(worker_key):
+ if connection is None:
+ connection = get_current_connection()
+ if not connection.exists(worker_key):
return None
name = worker_key[len(prefix):]
worker = cls([], name)
- queues = conn.hget(worker.key, 'queues')
- worker._state = conn.hget(worker.key, 'state') or '?'
+ queues = connection.hget(worker.key, 'queues')
+ worker._state = connection.hget(worker.key, 'state') or '?'
if queues:
worker.queues = map(Queue, queues.split(','))
return worker
Something went wrong with that request. Please try again.