python threading.Timer不会在指定时间立即启动

时间:2013-05-24 19:09:13

标签: python multithreading

我想每3秒执行一次功能 如果我调用一个没有参数的函数,代码就可以工作:

def mytempfunc():
    print "this is timer!"
    threading.Timer(5, mytempfunc).start()

但如果我用这样的参数调用一个函数:

def myotherfunc(a,b,c,d):
    print "this is timer!"
    threading.Timer(5, myotherfunc(a,b,c,d)).start()

将立即创建并启动新线程,而无需等待5秒钟。 有什么我错过的吗?

1 个答案:

答案 0 :(得分:11)

试试这个:

threading.Timer(5, myotherfunc, [a,b,c,d]).start()

在你的代码中,你实际上调用myotherfunc(a,b,c,d),而不是将你的函数和参数传递给Timer类。