From 744a51826db43d4f0b1d25f1374235f05b056d1c Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 2 Oct 2020 03:04:35 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Create=20first=20prototype=20of=20t?= =?UTF-8?q?he=20Baron=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalnet/baron/__init__.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 royalnet/baron/__init__.py diff --git a/royalnet/baron/__init__.py b/royalnet/baron/__init__.py new file mode 100644 index 00000000..d50e7394 --- /dev/null +++ b/royalnet/baron/__init__.py @@ -0,0 +1,36 @@ +from typing import * +import threading +import redis +import queue + + +__all__ = ( + "Baron", + "BaronListenerThread", +) + + +class Baron: + """The Baron module connects to a Redis database to send and receive messages.""" + + def __init__(self, + redis_args: Mapping[str, Any],): + self.publisher: redis.Redis = redis.Redis(**redis_args) + self.listen_thread: BaronListenerThread = BaronListenerThread(publisher=self.publisher) + self.was_started = False + + def listener(self) -> redis.client.PubSub: + return self.listen_thread.listener + + def start(self): + self.listen_thread.start() + self.was_started = True + + +class BaronListenerThread(threading.Thread): + def __init__(self, publisher: redis.Redis, *args, **kwargs): + super().__init__(*args, **kwargs) + self.listener: redis.client.PubSub = publisher.pubsub() + + def run(self) -> None: + self.listener.listen()