【问题标题】:python schedule jobs with different timezonespython调度具有不同时区的作业
【发布时间】:2018-05-01 13:45:18
【问题描述】:

我想安排一个 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()

我正在使用它通过烧瓶和外部 API 发布/接收一些东西。

Celery 或 heroku 调度程序或重的东西不是我想要的,debian(或 nix)环境的轻量级和 pythonic 将是理想的。我研究了schedulertzcronAPScheduler,但无法弄清楚我们如何将它们与时区一起使用。

我也尝试使用 crontab,但不知道如何在运行时添加作业,因为我也希望能够在运行时使用上述函数添加/删除作业。

我对 python 有一些经验,但这是我的第一个时区问题,我对此了解不多,所以如果我遗漏了什么或有其他方法,请随时赐教。

谢谢!

【问题讨论】:

  • 查看 Python 的 datetime 包。将国外时区转换为本地时区。另一种可能性是尝试使用crontab,并在单独的问题中发布您的问题。如果你在任何一个地方都能找到解决方案,你就赢了。
  • @Prune 我会试试的。似乎将外国时区转换为本地时区可以工作。我太笨了,谢谢你的建议

标签: python cron timezone scheduler schedule


【解决方案1】:

箭头库非常适合这一点,并且比标准日期/时间 (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)。

【讨论】:

    【解决方案2】:

    所描述的问题听起来像 python 的scheduler library 提供了一个开箱即用的解决方案,不需要用户进一步定制。 调度程序库被设计为可以在不同的时区调度作业,与创建调度程序的时区以及调度作业的独立时区无关。

    披露:我是调度程序库的作者之一

    为了演示,我将example 中的example 改编为问题:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多