From 0085656d20c76a3b7c2d5fd815890575c7054806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=85=89-k?= <36470587+kxgx@users.noreply.github.com> Date: Mon, 20 Jan 2025 18:26:50 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20weather.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/weather.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bin/weather.py b/bin/weather.py index 722d1b0..7cadd6f 100644 --- a/bin/weather.py +++ b/bin/weather.py @@ -60,6 +60,11 @@ def get_current_city(): time.sleep(180) # 重试前等待 # 注意:无限重试可能会在特定情况下导致程序无法终止,请确保在实际使用中考虑适当的退出条件或限制重试次数。 +def schedule_getWeath(): + """调度 getWeath 函数每3分钟执行一次""" + getWeath() + Timer(180, schedule_getWeath).start() # 重新设置定时器 + def getWeath(city='101060101'): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', @@ -78,7 +83,6 @@ def getWeath(city='101060101'): fileHandle = open('/root/2.13-Ink-screen-clock/bin/weather.json', 'w') fileHandle.write(str(Weath)) fileHandle.close() - Timer(180, getWeath).start() # 定时器函数,间隔三分钟下载文件至本地 print("天气文件更新") except requests.RequestException as e: logging.error("获取天气数据时出现网络错误: %s", e) @@ -86,7 +90,7 @@ def getWeath(city='101060101'): logging.error("发生了意外错误: %s", e) try: - getWeath() # 天气获取函数开始运行 + schedule_getWeath() # 开始调度天气获取函数 except OSError as e: logging.info(e) except KeyboardInterrupt: