File tree Expand file tree Collapse file tree 2 files changed +28
-25
lines changed Expand file tree Collapse file tree 2 files changed +28
-25
lines changed Original file line number Diff line number Diff line change 11"""Generic module containing utilities used for the GUI."""
22
33import zmq
4+ import time
45from typing import Optional
56
67
@@ -12,6 +13,7 @@ def is_port_free(port: int, zmq_context: Optional[zmq.Context] = None) -> bool:
1213 try :
1314 socket .bind (address )
1415 socket .unbind (address )
16+ time .sleep (0.1 )
1517 return True
1618 except zmq .error .ZMQError :
1719 return False
@@ -26,3 +28,28 @@ def select_zmq_port(zmq_context: Optional[zmq.Context] = None) -> int:
2628 port = socket .bind_to_random_port ("tcp://127.0.0.1" )
2729 socket .close ()
2830 return port
31+
32+
33+ def find_free_port (port : int , zmq_context : zmq .Context ):
34+ """Find free port to bind to.
35+
36+ Args:
37+ port: The port to start searching from.
38+ zmq_context: The ZMQ context to use.
39+
40+ Returns:
41+ The free port.
42+ """
43+ attempts = 0
44+ max_attempts = 10
45+ while not is_port_free (port = port , zmq_context = zmq_context ):
46+ if attempts >= max_attempts :
47+ raise RuntimeError (
48+ f"Could not find free port to display training progress after "
49+ f"{ max_attempts } attempts. Please check your network settings "
50+ "or use the CLI `sleap-train` command."
51+ )
52+ port = select_zmq_port (zmq_context = zmq_context )
53+ attempts += 1
54+
55+ return port
Original file line number Diff line number Diff line change 1212import matplotlib .transforms as mtransforms
1313from qtpy import QtCore , QtWidgets
1414
15- from sleap .gui .utils import is_port_free , select_zmq_port
15+ from sleap .gui .utils import find_free_port
1616from sleap .gui .widgets .mpl import MplCanvas
1717from sleap .nn .config .training_job import TrainingJobConfig
1818
@@ -788,30 +788,6 @@ def _setup_zmq(self, zmq_context: Optional[zmq.Context] = None):
788788 self .sub = self .ctx .socket (zmq .SUB )
789789 self .sub .subscribe ("" )
790790
791- def find_free_port (port : int , zmq_context : zmq .Context ):
792- """Find free port to bind to.
793-
794- Args:
795- port: The port to start searching from.
796- zmq_context: The ZMQ context to use.
797-
798- Returns:
799- The free port.
800- """
801- attempts = 0
802- max_attempts = 10
803- while not is_port_free (port = port , zmq_context = zmq_context ):
804- if attempts >= max_attempts :
805- raise RuntimeError (
806- f"Could not find free port to display training progress after "
807- f"{ max_attempts } attempts. Please check your network settings "
808- "or use the CLI `sleap-train` command."
809- )
810- port = select_zmq_port (zmq_context = self .ctx )
811- attempts += 1
812-
813- return port
814-
815791 # Find a free port and bind to it.
816792 self .zmq_ports ["publish_port" ] = find_free_port (
817793 port = self .zmq_ports ["publish_port" ], zmq_context = self .ctx
You can’t perform that action at this time.
0 commit comments