2015-09-22 53 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