Skip to content

Commit a4b9406

Browse files
committed
add no monitor
1 parent 090dab7 commit a4b9406

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/mantidprofiler/mantidprofiler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from mantidprofiler import __version__
2828
from mantidprofiler.diskrecord import monitor as diskmonitor
2929
from mantidprofiler.diskrecord import parse_log as parse_disk_log
30-
from mantidprofiler.psrecord import monitor as cpumonitor
30+
from mantidprofiler.psrecord import monitor as cpumonitor, no_monitor
3131
from mantidprofiler.psrecord import parse_log as parse_cpu_log
3232

3333

@@ -359,6 +359,8 @@ def main(argv=None):
359359
if not args.nocpu:
360360
# cpu monitor in main thread to prevent early exit
361361
cpumonitor(int(args.pid), logfile=args.logfile, interval=args.interval)
362+
else:
363+
no_monitor(int(args.pid), interval=args.interval)
362364

363365
# Read in algorithm timing log and build tree
364366
try:

src/mantidprofiler/psrecord.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,42 @@ def monitor(pid: int, logfile: Path, interval: Optional[float]) -> None:
144144
f.close()
145145

146146

147+
def no_monitor(pid: int, interval: Optional[float]) -> None:
148+
# change None to reasonable default
149+
if interval is None:
150+
interval = 0.0
151+
152+
pr = psutil.Process(pid)
153+
154+
# Record start time
155+
start_time = get_current_time()
156+
157+
try:
158+
# Start main event loop
159+
while True:
160+
# Find current time
161+
current_time = get_current_time()
162+
163+
try:
164+
pr_status = pr.status()
165+
except TypeError: # psutil < 2.0
166+
pr_status = pr.status
167+
except psutil.NoSuchProcess: # pragma: no cover
168+
break
169+
170+
# Check if process status indicates we should exit
171+
if pr_status in [psutil.STATUS_ZOMBIE, psutil.STATUS_DEAD]:
172+
print("Process finished ({0:.2f} seconds)".format(current_time - start_time))
173+
break
174+
175+
if interval > 0.0:
176+
sleep(interval)
177+
178+
except KeyboardInterrupt: # pragma: no cover
179+
print(f"killing process being monitored [PID={pr.pid}]:", " ".join(pr.cmdline()))
180+
pr.kill()
181+
182+
147183
# Parse the logfile outputted by psrecord
148184
def parse_log(filename: Path, cleanup: bool = True):
149185
"""

0 commit comments

Comments
 (0)