下面的代码实现每个月执行:其中调用了一个判断是否为最后一天的方法
import datetime import time import pymysql from isLastDayOfMonth import isLastDayMonth def doSth1(): # 链接数据库 conn = pymysql.Connect( host='192.0.9.169', port=5507, user='writer', passwd='Apsdf', db='api_data', charset='utf8' ) cur = conn.cursor() cur.execute('''select * from table2''') conn.commit() cur.close() conn.close() # 假装做这件事情需要一分钟 time.sleep(60) def main(h=23 , m=0): while isLastDayMonth(datetime.datetime.now()) ==1: while True: now = datetime.datetime.now() # 到达设定时间,结束内循环 if now.hour == h and now.minute == m: break # 不到时间就等20秒之后再次检测 time.sleep(20) # 做正事,一天做一次 doSth1() i