2012-07-17 69 views
9

如何在给定时间运行Python中的函数?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,()) 

我该怎么办?

+1

什么操作系统?您可能需要使用python外部的程序运行它,例如unix上的cron。 – 2012-07-17 13:51:31

+0

为什么不呢?你为“进入”设定了什么延迟(假设你尝试过)? – 2012-07-17 13:51:57

+0

你是如何尝试使用'sched.scheduler'的? – geoffspear 2012-07-17 13:52:52

回答

11

阅读从http://docs.python.org/py3k/library/sched.html文档:

从我们需要制定出一个延迟时间(单位:秒)去...

from datetime import datetime 
now = datetime.now() 

然后使用datetime.strptime解析“2012-07-17 15 :50:00' (我将离开这个格式字符串给你)

# I'm just creating a datetime in 3 hours... (you'd use output from above) 
from datetime import timedelta 
run_at = now + timedelta(hours=3) 
delay = (run_at - now).total_seconds() 

然后可以使用delay传递到threading.Timer实例,如:

threading.Timer(delay, self.update).start() 
+2

上本地时间日期时间码运算可能会失败。将本地时间转换为UTC或POSIX时间戳以执行计算。参见[查找是否在日期时间之间经过了24小时 - Python](http://stackoverflow.com/a/26313848/4279)。 – jfs 2015-02-18 23:46:31

+0

你可以用'total_seconds'直接'timedelta':'datetime.timedelta(小时= 3).total_seconds()' – Spela 2017-12-13 13:52:59

13

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

他们有一个例子只是这个用例: http://packages.python.org/APScheduler/dateschedule.html

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']) 
+1

没有关于Python 3工作 – Anarach 2017-06-27 10:17:13

2

我碰到了同样的问题:我可以没有得到与sched.enterabs注册的绝对时间事件被sched.run识别。如果我计算delaysched.enter对我有效,但由于我希望工作在特定时区的特定时间运行,因此使用起来很尴尬。

以我为例,我发现问题是在sched.scheduler初始默认timefunctime.time(如example),而是time.monotonictime.monotonic对于“绝对”时间表没有任何意义,因为从docs开始,“返回值的参考点未定义,因此只有连续调用结果之间的差异才有效。”

对我来说,解决办法是初始化调度程序

scheduler = sched.scheduler(time.time, time.sleep)

目前还不清楚你是否time_module.time实际上是了time.time或time.monotonic,但是当我正确初始化它,它工作正常。

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 

只是想找日/日期事件触发的时间: 只要日期“串”被绑定到一个更新的“时间”的字符串,它可以作为一个简单的TOD功能。您可以将字符串扩展为日期和时间。

无论是其字典顺序还是时间顺序比较, 只要字符串代表一个时间点,字符串也会。

有人好心提供此链接:

String Comparison Technique Used by Python

1

可能是值得安装这个库:https://pypi.python.org/pypi/schedule,基本上可以帮助你刚才所描述的一切。这里有一个例子:

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().monday.do(job) 
schedule.every().wednesday.at("13:15").do(job) 

while True: 
    schedule.run_pending() 
    time.sleep(1)