mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 03:24:20 +00:00
❇️ Raise an error if the Baron thread was already started
This commit is contained in:
parent
81429ddde5
commit
1dbefdf6b7
1 changed files with 15 additions and 2 deletions
|
@ -5,6 +5,8 @@ import redis
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
"Baron",
|
"Baron",
|
||||||
|
"BaronError",
|
||||||
|
"BaronAlreadyStartedError",
|
||||||
"BaronListenerThread",
|
"BaronListenerThread",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,14 +18,25 @@ class Baron:
|
||||||
redis_args: Mapping[str, Any],):
|
redis_args: Mapping[str, Any],):
|
||||||
self.publisher: redis.Redis = redis.Redis(**redis_args)
|
self.publisher: redis.Redis = redis.Redis(**redis_args)
|
||||||
self.listen_thread: BaronListenerThread = BaronListenerThread(publisher=self.publisher)
|
self.listen_thread: BaronListenerThread = BaronListenerThread(publisher=self.publisher)
|
||||||
self.was_started = False
|
self.is_started = False
|
||||||
|
|
||||||
def listener(self) -> redis.client.PubSub:
|
def listener(self) -> redis.client.PubSub:
|
||||||
return self.listen_thread.listener
|
return self.listen_thread.listener
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
"""Start the listen thread of the Baron module."""
|
||||||
|
if self.is_started:
|
||||||
|
raise BaronAlreadyStartedError("This Baron module was already started somewhere else.")
|
||||||
self.listen_thread.start()
|
self.listen_thread.start()
|
||||||
self.was_started = True
|
self.is_started = True
|
||||||
|
|
||||||
|
|
||||||
|
class BaronError(Exception):
|
||||||
|
"""An error of the Baron module."""
|
||||||
|
|
||||||
|
|
||||||
|
class BaronAlreadyStartedError(Exception):
|
||||||
|
"""This Baron module was already started somewhere else."""
|
||||||
|
|
||||||
|
|
||||||
class BaronListenerThread(threading.Thread):
|
class BaronListenerThread(threading.Thread):
|
||||||
|
|
Loading…
Reference in a new issue