文章内容

2022/2/9 15:07:25,作 者: 黄兵

PytzUsageWarning: The zone attribute is specific to pytz's interface

最近在使用 apscheduler 执行定时任务的时候,出现了如下警告:

E:\Code\project_name\venv\lib\site-packages\apscheduler\util.py:95: PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html

  if obj.zone == 'local':

E:\Code\project_name\venv\lib\site-packages\apscheduler\triggers\interval.py:66: PytzUsageWarning: The normalize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html

  return self.timezone.normalize(next_fire_time)

出现问题的原因:

没有在 APScheduler 中设置 PIP495 兼容时区,需要设置时区。

解决方案:

在 APScheduler 中设置 PIP495 兼容时区,具体代码示例如下:

import tzlocal

init_crawler_suspicious = ProcessCrawlerSuspicious()
# 实例化一个调度器
scheduler = BlockingScheduler(timezone=str(tzlocal.get_localzone()))
# 添加任务并设置触发方式每0.5小时执行一次
scheduler.add_job(init_crawler_suspicious.process_crawler_suspicious_run, 'interval', seconds=60)
# 开始运行调度器
try:
    scheduler.start()
except KeyboardInterrupt:
    sys.exit(0)

同时我们也可以用下面的写法,来解决这个问题:

# 实例化一个调度器
scheduler = BlockingScheduler(timezone="Asia/Shanghai")

关于地区代码,在此处:List of tz database time zones


参考资料:

1、PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - PytzUsageWarning: The zone attribute is specific to pytz's interface

分享到:

发表评论

评论列表