Skip to content

Commit 75d1345

Browse files
committed
refactor: watchable profile command
Signed-off-by: thxCode <thxcode0824@gmail.com>
1 parent 9228f85 commit 75d1345

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

gpustack_runtime/__main__.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import contextlib
44
import sys
5+
import time
56
from argparse import ArgumentParser
67

78
from gpustack_runtime import deployer, detector
@@ -39,6 +40,13 @@ def main():
3940
action="store_true",
4041
help="display available behaviors",
4142
)
43+
parser.add_argument(
44+
"--watch",
45+
"-w",
46+
type=int,
47+
help="continuously display available behaviors (used with --profile) in intervals of N seconds",
48+
default=0,
49+
)
4250

4351
# Register
4452
subcommand_parser = parser.add_subparsers(
@@ -57,7 +65,7 @@ def main():
5765
# Parse
5866
args = parser.parse_args()
5967
if getattr(args, "profile", False):
60-
profile()
68+
profile(getattr(args, "watch", 0))
6169
sys.exit(0)
6270
if not hasattr(args, "func"):
6371
parser.print_help()
@@ -68,23 +76,27 @@ def main():
6876
service.run()
6977

7078

71-
def profile():
72-
if sys.stdout.isatty():
79+
def profile(watch: int):
80+
try:
81+
while True:
82+
available_deployers: list[str] = []
83+
available_detectors: list[str] = []
84+
with contextlib.suppress(Exception):
85+
for dep in deployer.deployers:
86+
if dep.is_supported():
87+
available_deployers.append(dep.name)
88+
for det in detector.detectors:
89+
if det.is_supported():
90+
available_detectors.append(str(det.manufacturer))
91+
print("\033[2J\033[H", end="")
92+
print(f"Available Deployers: [{', '.join(available_deployers)}]")
93+
print(f"Available Detectors: [{', '.join(available_detectors)}]")
94+
if not watch:
95+
break
96+
time.sleep(watch)
97+
except KeyboardInterrupt:
7398
print("\033[2J\033[H", end="")
7499

75-
available_deployers: list[str] = []
76-
available_detectors: list[str] = []
77-
with contextlib.suppress(Exception):
78-
for dep in deployer.deployers:
79-
if dep.is_supported():
80-
available_deployers.append(dep.name)
81-
for det in detector.detectors:
82-
if det.is_supported():
83-
available_detectors.append(str(det.manufacturer))
84-
85-
print(f"Available Deployers: [{', '.join(available_deployers)}]")
86-
print(f"Available Detectors: [{', '.join(available_detectors)}]")
87-
88100

89101
if __name__ == "__main__":
90102
main()

0 commit comments

Comments
 (0)