-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirect_mode.py
More file actions
68 lines (56 loc) · 2.1 KB
/
Copy pathdirect_mode.py
File metadata and controls
68 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import json
import sys
import psutil
import time
import logging
from gpu_monitor import NvidiaPowerMonitor
logger = logging.getLogger(__name__)
# detecting interval defaulted at 0.1 second
def wait_for_process(process_name, interval=0.1):
gpu_monitor = NvidiaPowerMonitor()
"""
Waits for a process to start and then terminate.
Args:
process_name (str): The name of the process to monitor.
interval (int): The interval (in seconds) at which to check for the process.
"""
process_started = False
while True:
# Check if the process is running
for process in psutil.process_iter(['name']):
try:
if process.info['name'] == process_name:
if not process_started:
logger.info(f"Process {process_name} has started.")
process_started = True
gpu_monitor.start_monitoring()
break
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
continue
else:
if process_started:
logger.info(f"Process {process_name} has terminated.")
gpu_monitor.stop_monitoring()
return
time.sleep(interval)
def load_config(config_path):
"""
Load the configuration file.
Parameters:
config_path (str): The path to the configuration file.
Returns:
dict: The configuration data.
"""
with open(config_path, 'r') as file:
config = json.load(file)
return config
# Example usage
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
# have the main function pass which project to use
arg1 = sys.argv[1]
setting = load_config('conf.json')['Projects'][arg1]
process_name = setting['model_wrapping_settings']['process_running_name']
detect_interval = setting['model_wrapping_settings']['detecting_interval']
wait_for_process(process_name, )
print(f"Process {process_name} has started and terminated.")