2012-07-17 66 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)