2015-09-22 52 views
-1

我想在這裏安排任務每5秒,我做了什麼不工作:計劃任務在Python

conn = connect('mydatabase.db') 
c = conn.cursor() 
c.execute('CREATE TABLE IF NOT EXISTS RSSEntries (entry_id INTEGER PRIMARY KEY AUTOINCREMENT, title , url , date);') 


def checkLink(linko): 
    c.execute("SELECT entry_id FROM RSSEntries WHERE url = ?", (linko,)) 
    datas=c.fetchall() 

    if len(datas)==0: 
    return True 

    else: 
    return False 



def storeData(): 
    data = feedparser.parse("http://www...") 
    for i in range(len(data['entries'])): 

    if checkLink(data.entries[i].link) is True: 
     print "doesn't exist" 
     c.execute("insert into RSSEntries VALUES\ 
      (NULL,'%s', '%s', '%s')" % (data.entries[i].title,data.entries[i].link, data.feed.updated)) 

    else: 
     print "exist" 


schedule.every(5).seconds.do(storeData) 
conn.commit() 

storeData方法不可達.. 如果我運行storeData()代替schedule.every(5).seconds.do(storeData)代碼工作完美,我做錯了什麼

任何建議或其他方式來完成這項任務是受歡迎的。

回答

1

我認爲你缺少調度循環在腳本的末尾:

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

https://pypi.python.org/pypi/schedule

+0

它的工作謝謝你,你能解釋一下爲什麼'schedule.every(5).seconds。 do(storeData)'沒有工作 –

+0

by'schedule.every(5).seconds.do(storeData)'你只需要設置調度器對象的參數。如果不調用調度器方法'run_pending()',時鐘不會啓動 – lukaz