我想运行一个代码,每5秒运行一个带有参数的函数(例如,greet(h))。我尝试使用线程,但它不起作用。它只执行一次。请参阅下面的代码和错误:如下图所示在python中每隔X秒执行一个函数(带参数)
import threading
oh_hi = "Hi guys"
def greeting(hello):
print "%s" % hello
threading.Timer(1, greeting(oh_hi)).start()
错误:
> >>> ================================ RESTART
> ================================
> >>> Hi guys
> >>> Exception in thread Thread-1: Traceback (most recent call last):
> File "C:\Python27\lib\threading.py",
> line 530, in __bootstrap_inner
> self.run() File "C:\Python27\lib\threading.py", line
> 734, in run
> self.function(*self.args, **self.kwargs) TypeError: 'NoneType' object is not callable
敬请协助。
感谢
threading.Timer()只运行一次。它不运行每个X时隙。 – HongboZhu 2014-02-13 15:40:37