2013-05-27 31 views
0

我有,我有一個方法的Python類我想運行多個線程線程錯誤 - 參數過多

class OutageTool: 

def main(self): 
    outages = [{ 
     'var1' : 1, 
     'var2' : 2, 
    }, 
    { 
     'var1' : 3, 
     'var2' : 4, 
    }] 
    for outage in outages: 
      t = threading.Thread(target=self.outage_thread, args=(outage)) 
      t.start() 

def outage_thread(self, outage): 
    """ 
    some code here 
    """ 

的當我運行這段代碼,我發現了錯誤

TypeError: outage_thread() takes exactly 2 arguments (3 given) 

我是python的新手,非常感謝這裏發生的一切。

Ç

回答

1

您在創建Thread時已經忘記了,

在Python中(5)將導致整數5,一邊做(5,)就會變成元組一個條目是整數5

如果您將args=(outage)更改爲args=(outage,),它應該按預期工作。

2

使它

t = threading.Thread(target=self.outage_thread, args=(outage,)) 

(注意,(命令)outage之後)。在你的情況下,參數不是一個元組。

1

我相信錯誤可能在於args =(outage)不被視爲元組。

這應該解決它。

class OutageTool: 

    def main(self): 
     outages = [{ 
      'var1' : 1, 
      'var2' : 2, 
     }, 
     { 
      'var1' : 3, 
      'var2' : 4, 
     }] 
     for outage in outages: 
       t = threading.Thread(target=self.outage_thread, args=(outage,)) 
       t.start() 

def outage_thread(self, outage): 
    """ 
    some code here 
    """