Skip to content

Commit d3c431f

Browse files
authored
Super tiny refactor code for reuse (#505)
1 parent 41d3263 commit d3c431f

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

slime/ray/ray_actor.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
import ray
2-
from slime.utils.http_utils import is_port_available
1+
from slime.utils.misc import get_current_node_ip, get_free_port
32

43

54
class RayActor:
65
@staticmethod
76
def _get_current_node_ip_and_free_port(start_port=10000, consecutive=1):
8-
address = ray._private.services.get_node_ip_address()
9-
# strip ipv6 address
10-
address = address.strip("[]")
11-
12-
# find the port where port, port + 1, port + 2, ... port + consecutive - 1 are all available
13-
port = start_port
14-
while not all(is_port_available(port + i) for i in range(consecutive)):
15-
port += 1
16-
17-
return address, port
7+
return get_current_node_ip(), get_free_port(start_port=start_port, consecutive=consecutive)
188

199
def get_master_addr_and_port(self):
2010
return self.master_addr, self.master_port

slime/utils/misc.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import importlib
2+
import subprocess
3+
from typing import Optional
4+
5+
import ray
6+
7+
from slime.utils.http_utils import is_port_available
28

39

410
def load_function(path):
@@ -24,3 +30,31 @@ def __call__(cls, *args, **kwargs):
2430
instance = super().__call__(*args, **kwargs)
2531
cls._instances[cls] = instance
2632
return cls._instances[cls]
33+
34+
35+
def exec_command(cmd: str, capture_output: bool = False) -> Optional[str]:
36+
print(f"EXEC: {cmd}", flush=True)
37+
result = subprocess.run(
38+
["bash", "-c", cmd],
39+
shell=False,
40+
check=True,
41+
capture_output=capture_output,
42+
**(dict(text=True) if capture_output else {}),
43+
)
44+
if capture_output:
45+
return result.stdout
46+
47+
48+
def get_current_node_ip():
49+
address = ray._private.services.get_node_ip_address()
50+
# strip ipv6 address
51+
address = address.strip("[]")
52+
return address
53+
54+
55+
def get_free_port(start_port=10000, consecutive=1):
56+
# find the port where port, port + 1, port + 2, ... port + consecutive - 1 are all available
57+
port = start_port
58+
while not all(is_port_available(port + i) for i in range(consecutive)):
59+
port += 1
60+
return port

0 commit comments

Comments
 (0)