所以我得到了错误
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
我正在运行的代码是
import threading
def hello_world(a):
threading.Timer(2.0, hello_world(a)).start() # called every minute
print(a)
print("Hello, World!")
hello_world('a')
我注意到,当hello_world
函数中没有参数时,不会发生错误。但是,一旦我需要将参数传递给函数,我就会收到错误消息。有人可以解释为什么会这样以及如何解决吗?
该threading.Timer()
构造期望功能和参数传递给该函数作为单独的参数。调用它的正确方法是:
threading.Timer(2.0, hello_world, (a,)).start()
您可以看到我们hello_world
没有调用它就引用了,并且我们在1元组中列出了要分别传递的参数(a,)
。
你的方式目前正在做这件事,它的评估hello_world(a)
立即,它得到表达年底前,试图找出的返回值是什么hello_world(a)
将是-而不是启动计时器,然后每次计算表达式计时器熄灭。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句