【问题标题】:python threading.Timer start immediately not at specified timepython threading.Timer不在指定时间立即启动
【发布时间】:2013-05-20 11:20:27
【问题描述】:

我想每 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 秒。 有什么我错过的吗?

【问题讨论】:

标签: python multithreading


【解决方案1】:

试试这个:

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

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

【讨论】:

  • 优秀的捕获。我是 python 新手,不知道为什么代码会这样。
猜你喜欢
  • 2019-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
  • 2014-04-18
相关资源
最近更新 更多