Skip to content

Commit 6531447

Browse files
7174Andyeberrigan
andauthored
Fix port already in use when running inference (#2064)
* add wait * add comment for explanation * place find free port in utils * revert * add time when finding free ports * make the wait shorter * modify imports * solve error --------- Co-authored-by: Elizabeth <[email protected]>
1 parent 59e1f70 commit 6531447

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

sleap/gui/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Generic module containing utilities used for the GUI."""
22

33
import zmq
4+
import time
45
from 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

sleap/gui/widgets/monitor.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import matplotlib.transforms as mtransforms
1313
from 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
1616
from sleap.gui.widgets.mpl import MplCanvas
1717
from 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

0 commit comments

Comments
 (0)