Python代码实验调参需要等待1小时运行完成,自动将提前设置的5组参数(每组参数有8个)间隔1小时之后让Python代码再次自动依次调起运行其中的一组参数,每次跑完将实验结果写一个文件在本地存储通过邮件发送运行结果到指定QQ邮箱
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText# 假设的邮件配置(你需要替换为你的QQ邮箱SMTP配置)
smtp_server = 'smtp.qq.com'
smtp_port = 465 # SSL端口
sender_email = '@qq.com'
password = '' # 使用授权码
receiver_email = '@qq.com'# 假设的参数列表,实际中可以从文件或数据库读取
params_list = [{'param1': 'value1', 'param2': 'value2','param3': 'value3', 'param4': 'value4','param5': 'value5', 'param6': 'value6', 'param7': 'value7', 'param8': 'value8'}, # 8个参数# ... 其他4组参数
]def run_experiment(params):# 在这里编写您的实验代码# 假设实验结果是一个字符串result = f"Result for params: {params}"return resultdef send_email(result):msg = MIMEMultipart()msg['From'] = sender_emailmsg['To'] = receiver_emailmsg['Subject'] = 'Experiment Result'msg.attach(MIMEText(result, 'plain'))with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:server.login(sender_email, password)server.sendmail(sender_email, receiver_email, msg.as_string())def main(index):if index < len(params_list):params = params_list[index]result = run_experiment(params)with open(f'result_{index}.txt', 'w') as f:f.write(result)send_email(f'Experiment Result - Group {index}'+""+result)print(f"Experiment {index} finished. Waiting for 1 hour...")time.sleep(3600) # 等待1小时main(index + 1) # 递归调用以运行下一组参数if __name__ == "__main__":# 假设我们从命令行接收一个参数来指定从哪一组开始(例如,从0开始)import sysif len(sys.argv) > 1:start_index = int(sys.argv[1])main(start_index)else:main(0) # 默认从第一组开始