import threading
class MyThread(threading.Thread):
def __init__(self, target, *args):
self._des = target
self._args = args
threading.Thread.__init__(self,)
self._stopper = threading.Event()
def run(self):
self._des(*self._args)
def stop(self):
self._stopper.set()
def stopped(self):
return self._stopper.isSet()
def add(a,b):
print(a+b)
if __name__ == "__main__":
f1 = MyThread(add,1,2)
f1.start()
f1.join()
上面的代码在python2.7上完美运行但是当我用3.4尝试它时它显示以下错误
TypeError: add() missing 2 required positional arguments: 'a' and 'b'
有没有人可以帮我解决这个问题。 提前谢谢。
答案 0 :(得分:0)
保持简单,这个工作
def __init__(self, target, a,b):
self._des = target
self._a=a
self._b=b
threading.Thread.__init__(self)
self._stopper = threading.Event()
def run(self):
self._des(self._a,self._b)
顺便说一句,你需要先运行初始化,因为这会消除所有值,如下所示
def __init__(self, target, *args):
threading.Thread.__init__(self)
self._des = target
self._args = tuple(args)
self._stopper = threading.Event()
def run(self):
self._des(self._args[0],self._args[1])