代码
from apscheduler.schedulers.background import BackgroundScheduler
import requests
import datetimeurlweather = "http://localhost:8000/CrwalingViewWeather" # 天气接口
urlwater = "http://localhost:8000/CrwalingViewWater/" # 水质接口def fetch_weather():print("正在获取天气数据...", datetime.datetime.now())try:response = requests.get(urlweather)if response.status_code == 200:print("天气数据获取成功")else:print("天气数据获取失败")except Exception as e:print(f"获取天气数据时发生错误: {e}")def fetch_water_quality():print("正在获取水质数据...", datetime.datetime.now())try:response = requests.get(urlwater)if response.status_code == 200:print("水质数据获取成功")else:print("水质数据获取失败")except Exception as e:print(f"获取水质数据时发生错误: {e}")scheduler = BackgroundScheduler()# 每小时的第10分钟执行
scheduler.add_job(fetch_weather, 'cron', hour='*', minute=40)
# 每小时的第20分钟执行
scheduler.add_job(fetch_water_quality, 'cron', hour='*', minute=20)scheduler.start()try:# 模拟长时间运行while True:datetime.datetime.now()
except (KeyboardInterrupt, SystemExit):scheduler.shutdown()