代码之家  ›  专栏  ›  技术社区  ›  Learning is a mess

如何安排函数每天运行一次[[副本]

  •  0
  • Learning is a mess  · 技术社区  · 7 年前

    我有一个长时间运行的python脚本,我想在每天早上1:00做一些事情。

    我一直在看报纸 sched Timer

    0 回复  |  直到 12 年前
        1
  •  77
  •   sissi_luaty    7 年前

    您可以这样做:

    from datetime import datetime
    from threading import Timer
    
    x=datetime.today()
    y=x.replace(day=x.day+1, hour=1, minute=0, second=0, microsecond=0)
    delta_t=y-x
    
    secs=delta_t.seconds+1
    
    def hello_world():
        print "hello world"
        #...
    
    t = Timer(secs, hello_world)
    t.start()
    

    这将在第二天凌晨1点执行一个函数(如hello_world)。

    正如@PaulMag所建议的,更一般地说,为了检测是否由于月底的到来而必须重置月日,在这种情况下y的定义应如下所示:

    y = x.replace(day=x.day, hour=1, minute=0, second=0, microsecond=0) + timedelta(days=1)
    

    使用此修复程序,还需要将timedelta添加到导入中。其他代码行保持相同。因此,使用total_seconds()函数的完整解决方案是:

    from datetime import datetime, timedelta
    from threading import Timer
    
    x=datetime.today()
    y = x.replace(day=x.day, hour=1, minute=0, second=0, microsecond=0) + timedelta(days=1)
    delta_t=y-x
    
    secs=delta_t.total_seconds()
    
    def hello_world():
        print "hello world"
        #...
    
    t = Timer(secs, hello_world)
    t.start()
    
        2
  •  265
  •   user2099484    11 年前

    我花了相当多的时间在01:00启动了一个简单的Python程序 启动它和ApsScheduler似乎相当复杂,因为有些事情应该很简单。时间表( https://pypi.python.org/pypi/schedule )似乎是对的。

    您必须安装他们的Python库:

    pip install schedule
    

    这是根据他们的示例程序修改的:

    import schedule
    import time
    
    def job(t):
        print "I'm working...", t
        return
    
    schedule.every().day.at("01:00").do(job,'It is 01:00')
    
    while True:
        schedule.run_pending()
        time.sleep(60) # wait one minute
    

    您需要将自己的函数替换为作业,并使用nohup运行它,例如:

    nohup python2.7 MyScheduledProgram.py &
    

    如果重新启动,不要忘记再次启动。

        3
  •  18
  •   Paul Collingwood    9 年前

    APScheduler可能就是您想要的。

    from datetime import date
    from apscheduler.scheduler import Scheduler
    
    # Start the scheduler
    sched = Scheduler()
    sched.start()
    
    # Define the function that is to be executed
    def my_job(text):
        print text
    
    # The job will be executed on November 6th, 2009
    exec_date = date(2009, 11, 6)
    
    # Store the job in a variable in case we want to cancel it
    job = sched.add_date_job(my_job, exec_date, ['text'])
    
    # The job will be executed on November 6th, 2009 at 16:30:05
    job = sched.add_date_job(my_job, datetime(2009, 11, 6, 16, 30, 5), ['text'])
    

    https://apscheduler.readthedocs.io/en/latest/

        4
  •  10
  •   Ankit    10 年前

    我需要类似的东西来完成任务。这是我写的代码: 它计算第二天,将时间更改为所需的时间,并查找当前时间和下一个计划时间之间的秒数。

    import datetime as dt
    
    def my_job():
        print "hello world"
    nextDay = dt.datetime.now() + dt.timedelta(days=1)
    dateString = nextDay.strftime('%d-%m-%Y') + " 01-00-00"
    newDate = nextDay.strptime(dateString,'%d-%m-%Y %H-%M-%S')
    delay = (newDate - dt.datetime.now()).total_seconds()
    Timer(delay,my_job,()).start()