Skip to content

Instantly share code, notes, and snippets.

@rtt
Last active December 19, 2015 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtt/5875621 to your computer and use it in GitHub Desktop.
Save rtt/5875621 to your computer and use it in GitHub Desktop.
simple redis publisher
import redis
import threading
class Worker(threading.Thread):
'''Generic Main Worker class'''
def __init__(self, r, channels):
# init super
threading.Thread.__init__(self)
self.redis = r
# subscribe to given channels
self.pubsub = self.redis.pubsub()
self.pubsub.subscribe(channels)
def dispatch(self, *args, **kwargs):
'''Method for each worker to implement its "work"'''
raise NotImplementedError()
def run(self):
'''Iterates published messages and dispatches them'''
for item in self.pubsub.listen():
self.dispatch(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment