python threading.Timer 不在指定时间立即启动 [英] python threading.Timer start immediately not at specified time

查看:52
本文介绍了python threading.Timer 不在指定时间立即启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i want to execute a function in every 3 second the code works if i call a function without arguments like below:

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

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

but if i call a function with argument like this:

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

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

the new thread will be created and started immediately without waiting for 5 seconds. is there anything that i missed?

推荐答案

试试这个:

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

在您的代码中,您实际上调用了 myotherfunc(a,b,c,d),而不是将您的函数和参数传递给 定时器 类.

In your code, you actually call myotherfunc(a,b,c,d), rather than passing your function and arguments to the Timer class.

这篇关于python threading.Timer 不在指定时间立即启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆