Python脚本022:自动执行代码脚本

说明

为了提高GPU的利用率,该脚本在当前的GPU内存占用释放的时候自动运行下一个程序,真的牛!感谢mz大佬的分享,我只是一个无情的搬运工😀

import os
import time
import pynvml

pynvml.nvmlInit()  # 初始化
def watch_GPU(GPU_free=0.):
    # 设备情况
    deviceCount = pynvml.nvmlDeviceGetCount()
    print('显卡数量:', deviceCount)
    for i in range(deviceCount):
        handle = pynvml.nvmlDeviceGetHandleByIndex(i)
        gpu_name = pynvml.nvmlDeviceGetName(handle)
        print('GPU %d is :%s' % (i, gpu_name))

        # 显存信息
        memo_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
        print("GPU %d Memory Total: %.4f G" % (i, memo_info.total / 1024 / 1024 / 1000))
        GPU_free += memo_info.free / 1024 / 1024
        print("GPU %d Memory Free: %.4f G" % (i, memo_info.free / 1024 / 1024 / 1000))
        print("GPU %d Memory Used: %.4f G" % (i, memo_info.used / 1024 / 1024 / 1000))

        # 温度
        Temperature = pynvml.nvmlDeviceGetTemperature(handle, 0)
        print("Temperature is %.1f C" % (Temperature))

        # 风扇转速
        speed = pynvml.nvmlDeviceGetFanSpeed(handle)
        print("Fan speed is ", speed)

        # 电源状态
        power_ststus = pynvml.nvmlDeviceGetPowerState(handle)
        print("Power ststus", power_ststus)
        return GPU_free


def work_v2():
    # iter_list = ['0009', '0019', '0029', '0039', '0049', '0059', '0069', '0079', '0089', '0099', '0109', 'final', 'best']
    #
    # time.sleep(14400)
    # model_list = [0, 1]
    # for j in model_list:
    # for i in range(1):
        os.system(
            "python3 tools/train_net.py --config-file ./configs/VeRi/sbs_R50-ibn.yml")
        os.system(
            "python3 tools/train_net.py --config-file ./configs/VeRi/bagtricks.yml")
        os.system(
            "python3 tools/train_net.py --config-file ./configs/VeRi/MGN.yml")
        os.system(
            "python3 tools/train_net.py --config-file ./configs/VeRi/AGW.yml")
  

while True:
    if watch_GPU() > 5300:
        print(' ******  GPU is satisfied for our work. Get ready to start!  ******')
        try:
            work_v2()
        finally:
            pynvml.nvmlShutdown()
            exit()
    print('waiting for next time to detect ……')
    time.sleep(900)

在安装pynvml包的时候,使用conda install pynvml安装
因为用pip install pynvml的话会报没有匹配的版本

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值