15

I am writing a simple function that sends messages based on a schedule using AsyncIOScheduler.

scheduler = AsyncIOScheduler()
scheduler.add_job(job, "cron", day_of_week="mon-fri", hour = "16")
scheduler.start()

It seems to work, but I always get the following message:

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
PytzUsageWarning: The localize method is no longer necessary, as this time zone supports the fold attribute (PEP 495)

I am not familiar with time in python at all, so I am not really sure what this message is asking me to do. I visited the link provided in the message, but the explanation there is too complicated for me. As I understand, I have to migrate to using PEP495, but how exactly can I do just that?

Sergo055
  • 146
  • 1
  • 1
  • 8

4 Answers4

22

To set a PIP495 compatible timezone in APScheduler, set a parameter when instantiating the scheduler:

scheduler = AsyncIOScheduler(timezone="Europe/Berlin")
scheduler.add_job(job, "cron", day_of_week="mon-fri", hour = "16")
scheduler.start()

With flask-APScheduler (version 1.12.2), add the timezone to the configuration class:

"""Basic Flask Example from flask-apscheduler examples, file jobs.py"""
from flask import Flask
from flask_apscheduler import APScheduler


class Config:
    """App configuration."""
    JOBS = [
        {
            "id": "job1",
            "func": "jobs:job1",
            "args": (1, 2),
            "trigger": "interval",
            "seconds": 10,
        }
    ]
    SCHEDULER_API_ENABLED = True
    SCHEDULER_TIMEZONE = "Europe/Berlin"  # <========== add here


def job1(var_one, var_two):
    print(str(var_one) + " " + str(var_two))


if __name__ == "__main__":
    app = Flask(__name__)
    app.config.from_object(Config())

    scheduler = APScheduler()
    scheduler.init_app(app)
    scheduler.start()

    app.run()
Alex Poca
  • 2,406
  • 4
  • 25
  • 47
6

I was getting the same warning message because of the zone.

What I did was:

import tzlocal

scheduler = AsyncIOScheduler(timezone=str(tzlocal.get_localzone()))

With that local timezone I'm not getting the warning message anymore and it's running correctly.

Guilherme Matheus
  • 573
  • 10
  • 30
3

For now, you can't effectively suppress these warnings, because the problem comes from apscheduler itself.

In your particular case warning comes from here

if obj.zone == 'local':

The problem is pytz is considered deprecated in favor of zoneinfo module and its backports. So even if you set directly timezone argument as suggested before, you'll probably face the same warning from some other places until apsheduler would fix pytz usage for modern Python version.

But still, actually, there is something that you could do, but I'm not sure if such kind of fixes is acceptable for you.

PytzUsageWarning comes from pytz_deprecation_shim package which is the dependency of tzlocal. tzlocal is deeply integrated with pytz. As apscheduler has relatively relax dependency for tzlocal, you can install pretty old version of one, that hasn't such warning, but still acceptable for apsscheduler itself.

pip install tzlocal==2.1

But, please, pay attention it could break other project dependencies, so be careful.

Pavel Sapezhko
  • 661
  • 1
  • 6
  • 24
  • Thanks a lot! Actually, the warnings did get suppressed after setting a timezone parameter when initiating the scheduler. – Sergo055 Nov 05 '21 at 00:35
3

I just changed to

if __name__ == '__main__':
scheduler = BlockingScheduler(timezone=pytz.timezone('Asia/Ho_Chi_Minh'))
grey
  • 209
  • 3
  • 6