如何每一小时重置一次字典



是否可以每隔一小时重置一次python字典?

原文词典:

d = {1: 5, 2: 1, 3: 2}

每一小时,字典应为空(或应创建新字典)

d = {}

但是下面的程序不起作用

import time 
d = {}
d[1] = 5
d[2] = 1
d[3] = 2
i = 1
while i == 1:
    current = time.time()
    end = time.time()
    diff = end - current
    if diff == 60:
        d = {}
    print d
import time, threading
d = {1: 5, 2: 1, 3: 2}
def updates_dict():
    d = {}
    print "Updated on",time.ctime()
    threading.Timer(10*60*60, foo).start()
updates_dict()

您可以使用 threading 来实现。

import time
d = {1: 5, 2: 1, 3: 2}
start = time.time()
while True:
    diff = time.time() - start
    if not round(diff) % 60:
        d = {}
    print d

我建议看看APScheduler模块。它有很多定时事件选项。

相关内容

  • 没有找到相关文章

最新更新