我想運行一個代碼,每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