Skip to content

Commit a4d2a5c

Browse files
committed
Refactor get_hardware_info() to use async subprocess.run for improved concurrency
avoid Try-Catch using assertions
1 parent d82570d commit a4d2a5c

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

src/aleph/vm/orchestrator/machine.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1+
import asyncio
12
import json
23
import re
3-
import subprocess
44
from functools import lru_cache
55

66
import psutil
77

88

99
@lru_cache
10-
def get_hardware_info():
11-
lshw = subprocess.Popen(["lshw", "-sanitize", "-json"], stdout=subprocess.PIPE, shell=False)
12-
output, _ = lshw.communicate()
10+
async def get_hardware_info():
11+
lshw = await asyncio.create_subprocess_shell(
12+
"lshw -sanitize -json",
13+
stdout=asyncio.subprocess.PIPE,
14+
stderr=asyncio.subprocess.PIPE
15+
)
16+
17+
output, _ = await lshw.communicate()
1318
data = json.loads(output)
1419

15-
hw_info = {}
20+
hw_info = {
21+
"cpu": None,
22+
"memory": None
23+
}
1624

1725
for hw in data["children"][0]["children"]:
1826
if hw["id"] == "cpu":
@@ -24,8 +32,8 @@ def get_hardware_info():
2432

2533

2634
@lru_cache
27-
def get_cpu_info():
28-
hw = get_hardware_info()
35+
async def get_cpu_info():
36+
hw = await get_hardware_info()
2937

3038
cpu_info = hw["cpu"]
3139
architecture = cpu_info["width"]
@@ -53,20 +61,22 @@ def get_cpu_info():
5361

5462

5563
@lru_cache
56-
def get_memory_info():
57-
hw = get_hardware_info()
64+
async def get_memory_info():
65+
hw = await get_hardware_info()
5866
mem_info = hw["memory"]
5967

6068
memory_type = ""
6169
memory_clock = ""
6270

6371
for bank in mem_info["children"]:
6472
memory_clock = bank["clock"]
65-
try:
66-
memory_type = re.search("(DDR[2-6])", bank["description"]).group(0)
67-
break
68-
except:
69-
pass
73+
if "description" in bank:
74+
matched = re.search("(DDR[2-6])", bank["description"])
75+
if matched:
76+
memory_type = matched.group(0)
77+
break
78+
else:
79+
pass
7080

7181
return {
7282
"size": mem_info["size"],

src/aleph/vm/orchestrator/resources.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ class MachineCapability(BaseModel):
100100

101101

102102
@lru_cache
103-
def get_machine_properties() -> MachineProperties:
103+
async def get_machine_properties() -> MachineProperties:
104104
"""Fetch machine properties such as architecture, CPU vendor, ...
105105
These should not change while the supervisor is running.
106106
107107
In the future, some properties may have to be fetched from within a VM.
108108
"""
109109

110-
cpu_info = get_cpu_info()
110+
cpu_info = await get_cpu_info()
111111
return MachineProperties(
112112
cpu=CpuProperties(
113113
architecture=cpu_info["architecture"],
@@ -117,9 +117,9 @@ def get_machine_properties() -> MachineProperties:
117117

118118

119119
@lru_cache
120-
def get_machine_capability() -> MachineCapability:
121-
cpu_info = get_cpu_info()
122-
mem_info = get_memory_info()
120+
async def get_machine_capability() -> MachineCapability:
121+
cpu_info = await get_cpu_info()
122+
mem_info = await get_memory_info()
123123

124124
return MachineCapability(
125125
cpu=ExtendedCpuProperties(
@@ -161,15 +161,15 @@ async def about_system_usage(_: web.Request):
161161
start_timestamp=period_start,
162162
duration_seconds=60,
163163
),
164-
properties=get_machine_properties(),
164+
properties=await get_machine_properties(),
165165
)
166166
return web.json_response(text=usage.json(exclude_none=True), headers={"Access-Control-Allow-Origin:": "*"})
167167

168168

169169
async def about_capability(_: web.Request):
170170
"""Public endpoint to expose information about the CRN capability."""
171171

172-
capability: MachineCapability = get_machine_capability()
172+
capability: MachineCapability = await get_machine_capability()
173173
return web.json_response(text=capability.json(exclude_none=False), headers={"Access-Control-Allow-Origin:": "*"})
174174

175175

0 commit comments

Comments
 (0)