Skip to content

Instantly share code, notes, and snippets.

@athoune
Created November 2, 2018 16:16
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 athoune/050a20f32e3b9cbb6337260bd3a4afa6 to your computer and use it in GitHub Desktop.
Save athoune/050a20f32e3b9cbb6337260bd3a4afa6 to your computer and use it in GitHub Desktop.
aio pubsub pattern
from collections import defaultdict
from asyncio import gather
class Pubsub:
channels = defaultdict(list)
def add_handler(self, key: str, handler):
self.channels[key].append(handler)
async def handle(self, ctx, key: str, event):
await gather(*(handler(ctx, event) for handler in self.channels[key]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment