2013-08-18 60 views
1

我從來沒有見過這個錯誤之前:蟒蛇類型錯誤:不受約束的方法錯誤

TypeError: unbound method halt_listener() must be called with test_imports instance as first argument (got Queue instance instead)

我明白了,當我運行這段代碼:

class test_imports:#Test classes remove 
     alive = {'import_1': True, 'import_2': True}; 

     def halt_listener(self, control_Queue, thread_Name, kill_command): 
      while True: 
       print ("Checking queue for kill") 
       isAlive = control_queue.get() 
       print ("isAlive", isAlive) 
       if isAlive == kill_command: 
       print ("kill listener triggered") 
       self.alive[thread_Name] = False; 
       return 

     def import_1(self, control_Queue, thread_Number): 
      print ("Import_1 number %d started") % thread_Number 
      t = Thread(target=test_imports.halt_listener, args=(control_Queue, 'import_1', 't1kill')) 
      count = 0 
      t.run() 
      global alive 
      run = test_imports.alive['import_1']; 
      while run: 
       print ("Thread type 1 number %d run count %d") % (thread_Number, count) 
       count = count + 1 
       print ("Test Import_1 ", run) 
       run = self.alive['import_1']; 
      print ("Killing thread type 1 number %d") % thread_Number 

我認爲這是這一行def halt_listener(self, control_Queue, thread_Name, kill_command):特別是self,但我不確定有沒有人有任何指導 我應該如何處理這個?謝謝!

回答

5

您必須首先創建類的實例:

def import_1(self, control_Queue, thread_Number): 
    print ("Import_1 number %d started") % thread_Number 
    myinstance = test_imports() 
    t = Thread(target=myinstance.halt_listener, args=(control_Queue, 'import_1', 't1kill')) 
+0

好耶那的工作!現在快速的問題,爲什麼會這樣工作,只是在線程調用test_imports.halt_listener不是?僅供將來參考。感謝Bunch! 任何使用我的代碼的人都知道還有其他錯誤,我還沒有糾正。 –

+1

@KyleSponable因爲你必須在調用任何函數之前創建一個實例。請注意:http://ideone.com/SwkvY3。做'test_imports.halt_listener'將不起作用,因爲'halt_listener'函數沒有綁定到任何實例 – TerryA