Python - Start a Function at Given Time
如何在给定时间在Python中运行函数?
例如:
1 | run_it_at(func, '2012-07-17 15:50:00') |
它将在2012-07-17 15:50:00运行函数
我尝试了sched.scheduler,但它没有启动我的功能。
1 2 3 4 5 | 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, ()) |
我能做什么?
阅读http://docs.python.org/py3k/library/sched.html上的文档:
从那以后我们需要计算延迟(以秒为单位)......
1 2 | from datetime import datetime now = datetime.now() |
然后使用
1 2 3 4 | # 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() |
然后,您可以使用
1 | threading.Timer(delay, self.update).start() |
看一下Advanced Python Scheduler,APScheduler:http://packages.python.org/APScheduler/index.html
他们有一个这个用例的例子:
http://packages.python.org/APScheduler/dateschedule.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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']) |
以下是使用Python 2.7对stephenbez对APScheduler 3.5版的回答:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import os, time from apscheduler.schedulers.background import BackgroundScheduler from datetime import datetime, timedelta def tick(text): print(text + '! The time is: %s' % datetime.now()) scheduler = BackgroundScheduler() dd = datetime.now() + timedelta(seconds=3) scheduler.add_job(tick, 'date',run_date=dd, args=['TICK']) dd = datetime.now() + timedelta(seconds=6) scheduler.add_job(tick, 'date',run_date=dd, kwargs={'text':'TOCK'}) scheduler.start() print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')) try: # This is here to simulate application activity (which keeps the main thread alive). while True: time.sleep(2) except (KeyboardInterrupt, SystemExit): # Not strictly necessary if daemonic mode is enabled but should be done if possible scheduler.shutdown() |
可能值得安装这个库:https://pypi.python.org/pypi/schedule,基本上可以帮助您完成刚刚描述的所有内容。这是一个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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) |
我遇到了同样的问题:我无法通过
在我的例子中,我发现问题是
我的解决方案是将调度程序初始化为
目前还不清楚你的time_module.time实际上是time.time还是time.monotonic,但是当我正确初始化它时它工作正常。
1 2 3 4 5 6 7 8 | 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 |
只是寻找时间/日期事件触发器:
只要日期"string"与更新的"time"字符串相关联,它就可以作为一个简单的TOD函数。您可以将字符串扩展到日期和时间。
无论是词典排序还是时间顺序比较,
只要字符串代表一个时间点,字符串也会。
有人亲切地提供了这个链接:
Python使用的字符串比较技术