Created
November 2, 2018 16:16
-
-
Save athoune/050a20f32e3b9cbb6337260bd3a4afa6 to your computer and use it in GitHub Desktop.
aio pubsub pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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