diff --git a/sleap/gui/utils.py b/sleap/gui/utils.py index e141d4d6e..4f8215706 100644 --- a/sleap/gui/utils.py +++ b/sleap/gui/utils.py @@ -26,28 +26,3 @@ def select_zmq_port(zmq_context: Optional[zmq.Context] = None) -> int: port = socket.bind_to_random_port("tcp://127.0.0.1") socket.close() return port - - -def find_free_port(port: int, zmq_context: zmq.Context): - """Find free port to bind to. - - Args: - port: The port to start searching from. - zmq_context: The ZMQ context to use. - - Returns: - The free port. - """ - attempts = 0 - max_attempts = 10 - while not is_port_free(port=port, zmq_context=zmq_context): - if attempts >= max_attempts: - raise RuntimeError( - f"Could not find free port to display training progress after " - f"{max_attempts} attempts. Please check your network settings " - "or use the CLI `sleap-train` command." - ) - port = select_zmq_port(zmq_context=self.ctx) - attempts += 1 - - return port diff --git a/sleap/gui/widgets/monitor.py b/sleap/gui/widgets/monitor.py index 632adbad8..61fde2ff4 100644 --- a/sleap/gui/widgets/monitor.py +++ b/sleap/gui/widgets/monitor.py @@ -13,7 +13,7 @@ import matplotlib.transforms as mtransforms from qtpy import QtCore, QtWidgets -from sleap.gui.utils import find_free_port +from sleap.gui.utils import is_port_free, select_zmq_port from sleap.gui.widgets.mpl import MplCanvas from sleap.nn.config.training_job import TrainingJobConfig @@ -785,6 +785,30 @@ def _setup_zmq(self, zmq_context: Optional[zmq.Context] = None): self.ctx_given = zmq_context is not None self.ctx = zmq.Context() if zmq_context is None else zmq_context + def find_free_port(port: int, zmq_context: zmq.Context): + """Find free port to bind to. + + Args: + port: The port to start searching from. + zmq_context: The ZMQ context to use. + + Returns: + The free port. + """ + attempts = 0 + max_attempts = 10 + while not is_port_free(port=port, zmq_context=zmq_context): + if attempts >= max_attempts: + raise RuntimeError( + f"Could not find free port to display training progress after " + f"{max_attempts} attempts. Please check your network settings " + "or use the CLI `sleap-train` command." + ) + port = select_zmq_port(zmq_context=self.ctx) + attempts += 1 + + return port + # Progress monitoring, SUBSCRIBER self.sub = self.ctx.socket(zmq.SUB) self.sub.subscribe("")