Python-在给定时间启动函数

问题描述 投票:24回答:7

如何在给定时间在Python中运行函数?

例如:

run_it_at(func, '2012-07-17 15:50:00')

它将在2012-07-17 15:50:00运行函数func。>>

我尝试了sched.scheduler,但没有启动我的功能。

import time as time_module
scheduler = sched.scheduler(time_module.time, time_module.sleep)
t = time_module.strptime('2012-07-17 15:50:00', '%Y-%m-%d %H:%M:%S')
t = time_module.mktime(t)
scheduler_e = scheduler.enterabs(t, 1, self.update, ())

我该怎么办?

如何在给定的时间使用Python运行函数?例如:run_it_at(func,'2012-07-17 15:50:00'),它将在2012-07-17 15:50:00运行函数func。我尝试了sched.scheduler,但是...

python time scheduler
7个回答
20
投票

17
投票

看看高级Python调度程序APScheduler:http://packages.python.org/APScheduler/index.html


11
投票

这里是使用Python 2.7对APScheduler 3.5版的stephenbez答案的更新:


9
投票

可能值得安装此库:https://pypi.python.org/pypi/schedule,基本上可以帮助您完成刚刚描述的所有事情。这是一个例子:


3
投票

我遇到了同样的问题:我无法获得在sched.enterabs中注册的绝对时间事件以被sched.run识别。如果我计算了一个sched.enter,则delay为我工作,但是使用起来很尴尬,因为我希望作业在一天中的特定时间在特定时区运行。


1
投票
dateSTR = datetime.datetime.now().strftime("%H:%M:%S" )
if dateSTR == ("20:32:10"):
   #do function
    print(dateSTR)
else:
    # do something useful till this time
    time.sleep(1)
    pass

0
投票
import schedule
import time

def job():
    print("I'm working...")

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("11:25").do(job)
schedule.every().minute.at(":17").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)import schedule
© www.soinside.com 2019 - 2024. All rights reserved.