I have a Launcher
class that manages (check if they're alive and if not respawns them, also starts, etc.) Clusters
which are processes launched through the multiprocessing
Discord module.
To post relevant code:
class Launcher:
def __init__(self):
self._clusters = []
self._active = False
def _keep_alive(self):
while self._active:
for cluster in self._clusters:
...
time.sleep(1)
def start(self):
self._active = True
for i in range(5):
cluster = Cluster(str(i))
self._clusters.append(cluster)
self._keep_alive()
class Cluster:
def __init__(self, name):
self._name = name
self._process = None
def start(self):
self._process = multiprocessing.Process(target=bot.Bot, name=self._name, daemon=True)
self._process.start()
My question is fairly straightforward, I think designing this is easy but I'm not sure how to deal with it in a "Pythonic" way. The Launcher
should be able to get to check whether the process is_alive() or it's exitcode.
How should I expose the self._process
variable of Cluster
to the Launcher
class? Or exposing what it needs to function?
My original idea was to have two functions that returns the data the Launcher
requires:
def is_alive(self):
return self._process.is_alive()
@property
def exit_code(self):
return self._process.exitcode
But I feel the more information I need it would become bloated. Another idea I thought of is to just:
def process(self):
return self._process
Or just simply accessing cluster._process
from the Launcher
directly. I know there's no concept of private variables and it's up to the developer on how it's accessed and used which means I could also change whatever self._process
is outside of the class.
What would make the most sense in these type of cases?