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?
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()
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.
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.
I just changed to
if __name__ == '__main__':
scheduler = BlockingScheduler(timezone=pytz.timezone('Asia/Ho_Chi_Minh'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With