我看到并阅读了关于堆栈的回答问题,但我仍然不知道如何修复它。
请帮忙我很高兴。
下面是我的代码:#!/usr/local/bin/python
import threading
class TaskManagmentHandler:
# Handle tasks from server
MINUTES_TO_FIRST_TASK = 5
MINUTES_TO_NORMAL_TASK = 20
MINUTES_TO_FAILED_TASK = 20
global currentAwaitingTime
currentAwaitingTime = MINUTES_TO_FIRST_TASK
def executeTaskFromServer(self):
print ("hi!")
self.currentAwaitingTime = self.MINUTES_TO_NORMAL_TASK
taskThread = threading.Timer(self.currentAwaitingTime, self.executeTaskFromServer())
taskThread.start()
# start normal task after 5 minutes
# start cycled task every 20 minutes (task call itself after 20 minutes)
if __name__ == "__main__":
print ("hello!")
taskThread = threading.Timer(currentAwaitingTime, executeTaskFromServer)
taskThread.start()
这里是我遇到的错误:
hello!
Exception in thread Thread-1:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 1082, in run
self.function(*self.args, **self.kwargs)
TypeError: executeTaskFromServer() takes exactly 1 argument (0 given)
Process finished with exit code 0
即使我在executeTaskFromServer
中标记所有代码并只是打印'hi',我仍然有同样的问题。
我甚至尝试了class TaskManagmentHandler():
,但它没有解决我的问题。
您忘记了self
(因为您的代码在方法下缩进)
taskThread = threading.Timer(currentAwaitingTime, self.executeTaskFromServer)
但这是你实际上应该做的,将代码移出类并创建一个新对象,然后调用executeTaskFromServer
方法
if __name__ == "__main__":
print ("hello!")
task_mgr = TaskManagmentHandler()
task_mgr.executeTaskFromServer()
你只需要启动线程一次