所以,我有这个代码:
t = threading.Timer(570.0, reddit_post(newmsg))
t.start()
开始一个快速的Reddit帖子。 可悲的是,它不是等待570秒,而是自动执行reddit_post而无需实际等待。
我该怎么做才能解决这个问题?
这是因为Timer
当你说t = threading.Timer(570.0, reddit_post(newmsg))
你需要做的是这样的:
threading.Timer(570.0, reddit_post, [newmsg]).start()
请参阅 Timer 类的文档
更详细地解释一下:
调用 Timer 构造函数时,应为其提供三个参数。第一个参数应该是您希望计时器等待多长时间。第二个参数应该是可调用的(例如函数)。第三个参数应该是用于调用函数的参数列表。
举个例子。
# First we define a function to call.
def say_hello(name):
print('hello ' + name)
# Now we can call this function.
say_hello('john')
# But when we make a timer to call it later, we do not call the function.
timer = threading.Timer(10, say_hello, ['john'])
timer.start()