You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python schedule模块实现Telegram bot定时任务无响应问题求助

Python schedule定时任务未执行的问题排查与解决

你的核心问题是只定义了定时任务,但没有启动schedule的任务调度循环,schedule模块不会自动后台运行,必须主动让它持续检查到期任务。

具体解决步骤:

  • 添加调度循环:在代码末尾加入持续检查的循环,调用schedule.run_pending()来触发到期任务,同时用time.sleep()控制检查间隔,避免占用过多CPU资源。
  • 确保时区依赖库安装:使用时区参数时,需要先安装pytz库,执行pip install pytz即可,否则时区设置会失效。
  • 测试短周期任务:如果要快速验证,建议用分钟级的定时规则,比如schedule.every(2).minutes.do(...),比指定具体时间更方便测试。

修改后的完整测试代码:

import schedule
import time

def test_schedule(test_string):
    print(test_string)

# 马德里时间13点执行任务(需确保已安装pytz)
schedule.every().day.at("13:00", "Europe/Madrid").do(test_schedule, "test")

# 启动调度循环
while True:
    schedule.run_pending()
    time.sleep(1)

如果是测试2分钟后执行的场景,把任务定义改成:

schedule.every(2).minutes.do(test_schedule, "test")

内容的提问来源于stack exchange,提问作者Aobies59

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 09:25:55