def add_listener(self, keys: Union[Key, Set[Key]], callback: ListenerCallback) -> None:
'''
Attach ``callback`` to one or more keys. If more than one key is provided, the callback will be
reading all messages from one queue, and they are guaranteed to arrive in the same order they were published.
:param keys: One key, or a set of keys.
'''
keys, sub_id = self._get_listener_subscription(keys, callback)
for key in keys:
self.subscribe(key, sub_id)
async def consumer() -> None:
key, msg = await self.consume(sub_id)
callback(key, msg)
loop = aiopubsub.loop.Loop(consumer, delay = None)
loop.start()
self._listeners[sub_id] = Listener(loop, keys)
评论列表
文章目录