python 安排不同时区的作业
python schedule jobs with different timezones
我想安排一个 python 函数在每天的特定时间 运行 为不同时区的客户列表。
这基本上就是我想要做的:
import schedule
import time
def job(text):
print("Hello " + text)
def add_job(user_tz, time, text):
schedule.every().day.at(time).do(job(text))
# the above adds all jobs at local time, I want to use different timezones with these
def run_job():
while(1):
schedule.run_pending()
time.sleep(1)
if __name__=='__main__':
add_job('America/New_York', "12:00", 'New York')
add_job('Europe/London', "12:00", 'London')
run_job()
我将其用于 posting/receiving 一些使用烧瓶和外部 API 的东西。
Celery 或 heroku 调度程序或重的东西不是我要找的东西,轻量级的东西和 pythonic for debian(or nix) env 是理想的。我研究了 scheduler, tzcron and APScheduler,但无法弄清楚我们如何将它们与时区一起使用。
我也尝试使用 crontab 但无法弄清楚如何在 运行 时间添加作业,因为我希望能够 add/remove 使用上述功能在 运行 作业]还有时间。
我对python有一些经验,但这是我遇到的第一个时区问题,我对此了解不多,所以如果我遗漏了什么或者还有其他问题,请随时赐教这样做的方式。
谢谢!
箭头库对此非常有用,并且比标准 date/time (imo) 简单得多。 arrow docs.
import arrow
from datetime import datetime
now = datetime.now()
atime = arrow.get(now)
print(now)
print (atime)
eastern = atime.to('US/Eastern')
print (eastern)
print (eastern.datetime)
2017-11-17 09:53:58.700546
2017-11-17T09:53:58.700546+00:00
2017-11-17T04:53:58.700546-05:00
2017-11-17 04:53:58.700546-05:00
我会更改您的 "add_job" 方法,将我所有的传入日期修改为标准时区(例如 utc)。
所描述的问题听起来像是 scheduler library for python 提供了开箱即用的解决方案,不需要用户进一步自定义。
调度器库的设计使得作业可以在不同的时区进行调度,它与创建调度器的时区无关,也与作业调度在哪个独立的时区无关。
披露:我是调度程序库的作者之一
为了演示,我对问题进行了 example from the documentation 修改:
import datetime as dt
from scheduler import Scheduler
import scheduler.trigger as trigger
# Create a payload callback function
def useful():
print("Very useful function.")
# Instead of setting the timezones yourself you can use the `pytz` library
tz_new_york = dt.timezone(dt.timedelta(hours=-5))
tz_wuppertal = dt.timezone(dt.timedelta(hours=2))
tz_sydney = dt.timezone(dt.timedelta(hours=10))
# can be any valid timezone
schedule = Scheduler(tzinfo=dt.timezone.utc)
# schedule jobs
schedule.daily(dt.time(hour=12, tzinfo=tz_new_york), useful)
schedule.daily(dt.time(hour=12, tzinfo=tz_wuppertal), useful)
schedule.daily(dt.time(hour=12, tzinfo=tz_sydney), useful)
# Show a table overview of your jobs
print(schedule)
max_exec=inf, tzinfo=UTC, priority_function=linear_priority_function, #jobs=3
type function due at tzinfo due in attempts weight
-------- ---------------- ------------------- ------------ --------- ------------- ------
DAILY useful() 2021-07-20 12:00:00 UTC-05:00 1:23:39 0/inf 1
DAILY useful() 2021-07-21 12:00:00 UTC+10:00 10:23:39 0/inf 1
DAILY useful() 2021-07-21 12:00:00 UTC+02:00 18:23:39 0/inf 1
使用简单循环执行作业:
import time
while True:
schedule.exec_jobs()
time.sleep(1) # wait a second
我想安排一个 python 函数在每天的特定时间 运行 为不同时区的客户列表。
这基本上就是我想要做的:
import schedule
import time
def job(text):
print("Hello " + text)
def add_job(user_tz, time, text):
schedule.every().day.at(time).do(job(text))
# the above adds all jobs at local time, I want to use different timezones with these
def run_job():
while(1):
schedule.run_pending()
time.sleep(1)
if __name__=='__main__':
add_job('America/New_York', "12:00", 'New York')
add_job('Europe/London', "12:00", 'London')
run_job()
我将其用于 posting/receiving 一些使用烧瓶和外部 API 的东西。
Celery 或 heroku 调度程序或重的东西不是我要找的东西,轻量级的东西和 pythonic for debian(or nix) env 是理想的。我研究了 scheduler, tzcron and APScheduler,但无法弄清楚我们如何将它们与时区一起使用。
我也尝试使用 crontab 但无法弄清楚如何在 运行 时间添加作业,因为我希望能够 add/remove 使用上述功能在 运行 作业]还有时间。
我对python有一些经验,但这是我遇到的第一个时区问题,我对此了解不多,所以如果我遗漏了什么或者还有其他问题,请随时赐教这样做的方式。
谢谢!
箭头库对此非常有用,并且比标准 date/time (imo) 简单得多。 arrow docs.
import arrow
from datetime import datetime
now = datetime.now()
atime = arrow.get(now)
print(now)
print (atime)
eastern = atime.to('US/Eastern')
print (eastern)
print (eastern.datetime)
2017-11-17 09:53:58.700546
2017-11-17T09:53:58.700546+00:00
2017-11-17T04:53:58.700546-05:00
2017-11-17 04:53:58.700546-05:00
我会更改您的 "add_job" 方法,将我所有的传入日期修改为标准时区(例如 utc)。
所描述的问题听起来像是 scheduler library for python 提供了开箱即用的解决方案,不需要用户进一步自定义。 调度器库的设计使得作业可以在不同的时区进行调度,它与创建调度器的时区无关,也与作业调度在哪个独立的时区无关。
披露:我是调度程序库的作者之一
为了演示,我对问题进行了 example from the documentation 修改:
import datetime as dt
from scheduler import Scheduler
import scheduler.trigger as trigger
# Create a payload callback function
def useful():
print("Very useful function.")
# Instead of setting the timezones yourself you can use the `pytz` library
tz_new_york = dt.timezone(dt.timedelta(hours=-5))
tz_wuppertal = dt.timezone(dt.timedelta(hours=2))
tz_sydney = dt.timezone(dt.timedelta(hours=10))
# can be any valid timezone
schedule = Scheduler(tzinfo=dt.timezone.utc)
# schedule jobs
schedule.daily(dt.time(hour=12, tzinfo=tz_new_york), useful)
schedule.daily(dt.time(hour=12, tzinfo=tz_wuppertal), useful)
schedule.daily(dt.time(hour=12, tzinfo=tz_sydney), useful)
# Show a table overview of your jobs
print(schedule)
max_exec=inf, tzinfo=UTC, priority_function=linear_priority_function, #jobs=3
type function due at tzinfo due in attempts weight
-------- ---------------- ------------------- ------------ --------- ------------- ------
DAILY useful() 2021-07-20 12:00:00 UTC-05:00 1:23:39 0/inf 1
DAILY useful() 2021-07-21 12:00:00 UTC+10:00 10:23:39 0/inf 1
DAILY useful() 2021-07-21 12:00:00 UTC+02:00 18:23:39 0/inf 1
使用简单循环执行作业:
import time
while True:
schedule.exec_jobs()
time.sleep(1) # wait a second