【问题标题】:Problem with job queue in python telegram botpython电报机器人中的作业队列问题
【发布时间】:2021-04-03 10:40:20
【问题描述】:
y=str("12:50AM")+"+0000"
bot_refresh_time = datetime.strptime(y,'%I:%M%p%z').timetz()
bot_refresh_time=bot_refresh_time.replace(tzinfo = tz)
updater.job_queue.run_once(bot_bal,bot_refresh_time,name="daily_check_task")

上面的代码执行没有错误,它被添加到作业队列但没有调用回调。

如果我将 run_once 更改为 run_daily,它可以正常工作 即updater.job_queue.run_daily(bot_bal,bot_refresh_time,name="daily_check_task")

如果上述代码中的时区为“None”(tzinfo=None),那么 run_once 工作得非常好,所以我认为问题出在时区上,但如果是这种情况,那么 run_daily 不应该工作,因为它们都使用 datetime.time 作为语法中的时间

python电报机器人的作业队列run_once的语法。

python电报机器人的作业队列run_daily的语法。

任何建议或答案都会有所帮助。我只是编码的初学者:)

编辑: run_once 有效,但不是将给定时间设置为时区 (bot_refresh_time=bot_refresh_time.replace(tzinfo = tz)),而是将时间转换为给定时区(即,如果我的时区是亚洲/加尔各答,给定时间是 01:00AM ,在使用 run_once 时,作业在 IST 上午 06:30 而不是 IST 上午 01:00 执行)。这个问题只在使用run_once时出现,run_daily

没有问题

【问题讨论】:

    标签: python telegram-bot python-telegram-bot telegram-api


    【解决方案1】:

    从 13.0 版开始,JobQueue 只能处理 pytz 时区,因为 PTB 使用库 APScheduler 来处理 JobQueue。请注意,不再支持旧版本。 run_once 的一个工作示例是:

    import datetime as dtm
    import pytz
    
    from telegram.ext import Updater
    
    updater = Updater('TOKEN')
    
    
    def test(_):
        print('running at', dtm.datetime.utcnow())
    
    
    time = pytz.timezone('Asia/Kolkata').localize(dtm.datetime.now() + dtm.timedelta(seconds=5))
    updater.job_queue.run_once(test, time, name="daily_check_task")
    updater.start_polling()
    updater.idle()
    

    【讨论】:

    • 谢谢!这在 13.5 版中仍然是一个问题。将pytz.timezone('<timezone>').localize( ) 包裹在我的datetime 周围,对象固定run_once!顺便提一句。可以使用pytz.all_timezones 列出时区。
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2019-04-03
    相关资源
    最近更新 更多