如何每隔一小时重置字典

时间:2016-06-07 12:03:21

标签: python dictionary time

是否可以每隔一小时重置一次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

3 个答案:

答案 0 :(得分:1)

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实现。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我建议您查看the APScheduler module。它为定时事件提供了很多选择。