我有一個類的功能,我想作爲一個線程啓動。該函數將元組值作爲其參數。該函數工作正常,但我的初始設置引發TypeError。下面是一些示例代碼:如何將元組值作爲參數發送給作爲線程啓動的函數?
import threading
class Test:
def __init__(self):
t = threading.Thread(target=self.msg, args=(2,1))
t.start()
print "started thread"
# msg takes a tuple as its arg (e.g. tupleval = (0,1))
def msg(self,tupleval):
if(tupleval[0] > 1):
print "yes"
else:
print "no"
test = Test()
test.msg((2,2))
test.msg((0,0))
,然後輸出如下:
started thread
yes
no
Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 484, in run
self.__target(*self.__args, **self.__kwargs)
TypeError: msg() takes exactly 2 arguments (3 given)
這似乎是在結束了兩個顯式調用工作,但初始設置呼叫拋出類型錯誤。我已經嘗試以各種方式將值包裝成元組,但無法擺脫錯誤。想法?