Skip to content

Instantly share code, notes, and snippets.

@Ivoz
Created February 6, 2012 15:46
Show Gist options
  • Save Ivoz/1752768 to your computer and use it in GitHub Desktop.
Save Ivoz/1752768 to your computer and use it in GitHub Desktop.
import gevent
class Publisher(object):
def __init__(self):
self.channels = set()
self.subscribers = set()
self.subscriptions = {}
self.publications = {}
def distribute(self, channel):
def publication():
for article in channel:
for sub in self.subscriptions[hash(channel)]:
sub.put(article)
self.publications[hash(channel)] = gevent.spawn(publication)
def subscribe(self, subscriber, channel):
self.subscriptions[hash(channel)].add(subscriber)
def unsubscribe(self, subscriber, channel):
del self.subscriptions[hash(channel)].remove(subscriber)
def publish(self, channel):
self.subscriptions[hash(channel)] = set()
def unpublish(self, channel):
del self.subscriptions[hash(channel)]
self.channels.remove(channel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment