使用python定时跑模型

1. 根据时间定时

import datetime
import os

if __name__ == "__main__":
    current_time = datetime.datetime.now()
    target_time_str = '2023-04-03 14:15:00'
    target_time = datetime.datetime.strptime(target_time_str, '%Y-%m-%d %H:%M:%S')
    while target_time > current_time:
        current_time = datetime.datetime.now()
    else:
        os.system('CUDA_VISBLE_DEVICES=1 python train_fusion.py')

2. 根据gpu显存剩余量定时

import subprocess
import os

if __name__ == "__main__":
    # 定义 nvidia-smi 命令行命令
    command = "nvidia-smi -i 0 --query-gpu=memory.used,memory.free --format=csv -l 1"
    memory_set = 200000
    # 运行命令行命令,并实时获取输出信息
    process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    while True:
        output = process.stdout.readline().decode()
        if output == '' and process.poll() is not None:
            break
        if output.strip() != "memory.used [MiB], memory.free [MiB]":
            res_gpu = output.strip().split(',')[-1].strip()[:-4]
            if int(res_gpu) >= memory_set:
                os.system('CUDA_VISBLE_DEVICES=1 python train_fusion.py')
                break

你可能感兴趣的:(python)