ACCA225 commited on
Commit
2777c8d
1 Parent(s): f8c9d8f

Update app2.py

Browse files
Files changed (1) hide show
  1. app2.py +16 -9
app2.py CHANGED
@@ -222,16 +222,23 @@ import os
222
  import threading
223
  import wandb
224
  import time
225
- def wandb():
226
- # Assuming you have initialized WandB before this function call
227
- # ...
228
-
229
  while True:
230
- # Loop to continuously monitor GPU temperature and usage
231
- gpu_temperature_command = "nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader,nounits"
232
- gpu_temperature = int(os.popen(gpu_temperature_command).read().strip())
 
 
 
 
 
 
 
 
 
 
 
233
 
234
- wandb.log({"GPU 温度": gpu_temperature})
235
  time.sleep(60)
236
 
237
  def start():
@@ -248,7 +255,7 @@ def start():
248
  # 在这里处理异常的代码
249
  print(f"An error occurred: {e}")
250
  # Create threads for each function
251
- wandb_thread = threading.Thread(target=wandb)
252
  start_thread = threading.Thread(target=start)
253
 
254
  # Start the threads
 
222
  import threading
223
  import wandb
224
  import time
225
+ def monitor_gpu():
 
 
 
226
  while True:
227
+ try:
228
+ # 获取 GPU 温度
229
+ handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0) # 0 表示第一个 GPU
230
+ gpu_temperature = nvidia_smi.nvmlDeviceGetTemperature(handle, nvidia_smi.NVML_TEMPERATURE_GPU)
231
+
232
+ # 获取 GPU 使用率
233
+ utilization = nvidia_smi.nvmlDeviceGetUtilizationRates(handle)
234
+ gpu_usage = utilization.gpu
235
+
236
+ # 使用 WandB 记录 GPU 温度和使用率
237
+ wandb.log({"GPU 温度": gpu_temperature, "GPU 使用率": gpu_usage})
238
+
239
+ except Exception as e:
240
+ print(f"Error: {e}")
241
 
 
242
  time.sleep(60)
243
 
244
  def start():
 
255
  # 在这里处理异常的代码
256
  print(f"An error occurred: {e}")
257
  # Create threads for each function
258
+ wandb_thread = threading.Thread(target=monitor_gpu)
259
  start_thread = threading.Thread(target=start)
260
 
261
  # Start the threads