2013-08-01 45 views
0

我試圖讓worker_manager工作,但我不斷收到此錯誤:AttributeError的:「詮釋」對象有沒有屬性「GETATTR」

Traceback (most recent call last): 
    File "multiQueue2.py", line 61, in <module> 
    manager.generate(control, threadName, i) 
    File "multiQueue2.py", line 38, in generate 
    target = i.getattr(name) 
AttributeError: 'int' object has no attribute 'getattr' 

這裏是代碼我有,worker_manager工作,是唯一會上演的部分。它應該從字典中取出線程名稱,然後訪問關聯的類。有人建議嗎?謝謝!

import multiprocessing 
import time 

class test_imports:#Test classes remove 
     def import_1(self, control_queue, thread_number): 
      print ("Import_1 number %d started") % thread_number 
      run = True 
      count = 1 
      while run: 
       alive = control_queue.get() 
       if alive == 't1kill': 
        print ("Killing thread type 1 number %d") % thread_number 
        run = False 
        break 
       print ("Thread type 1 number %d run count %d") % (thread_number, count) 
       count = count + 1 

     def import_2(self, control_queue, thread_number): 
      print ("Import_1 number %d started") % thread_number 
      run = True 
      count = 1 
      while run: 
       alive = control_queue.get() 
       if alive == 't2kill': 
        print ("Killing thread type 2 number %d") % thread_number 
        run = False 
        break 
       print ("Thread type 2 number %d run count %d") % (thread_number, count)   
       count = count + 1 

class worker_manager: 
    # ... 
    names = {'one': 'import_1', 'two': 'import_2'} 
    def __init__(self): 
     self.children = {} 
    def generate(self, control_queue, threadName, runNum): 
     name = self.names[threadName] 
     target = i.getattr(name) #THis is throwing the error 
     print ("Starting %s number %d") % (name, runNum) 
     p = multiprocessing.Process(target=target, args=(control_queue, runNum)) 
     self.children[threadName] = p 
     p.start() 
    def terminate(self, threadName): 
     self.children[threadName].join() 

if __name__ == '__main__': 
    # Establish communication queues 
    control = multiprocessing.Queue() 
    manager = worker_manager()  
    runNum = int(raw_input("Enter a number: ")) 
    threadNum = int(raw_input("Enter number of threads: ")) 
    threadName = raw_input("Enter number: ") 
    thread_Count = 0 

    print ("Starting threads") 

    for i in range(threadNum): 
     if threadName == 'three': 
      manager.generate(control, 'one', i) 
      manager.generate(control, 'two', i) 
     manager.generate(control, threadName, i) 
     thread_Count = thread_Count + 1    
     if threadName == 'three': 
      thread_Count = thread_Count + 1 

    time.sleep(runNum)#let threads do their thing 

    print ("Terminating threads")  

    for i in range(thread_Count): 
     control.put("t1kill") 
     control.put("t2kill") 
    if threadName == 'three': 
     manager.terminate('one') 
     manager.terminate('two') 
    else: 
     manager.terminate(threadName) 

回答

4
def generate(self, control_queue, threadName, runNum): 
    name = self.names[threadName] 
    target = i.getattr(name) #THis is throwing the error 

i這裏就不在本地範圍內定義。這意味着它是全球定義的。這意味着你所指的變量是這裏定義的變量:

for i in range(threadNum): 

如果這是故意的,這是不好的做法。儘量避免使用全局變量。 另外,這是一個整數。您正在嘗試:

i.getattr(name) 

上的整數。那應該做什麼?有一個叫做getattr的函數來獲得動態定義的屬性,但整數沒有任何動態屬性,所以目前還不清楚你想要做什麼。

+0

我想使用字典來引用函數調用。我應該用什麼來代替target = i.getattr(name) –

+0

@KyleSponable你應該用實際包含你需要屬性的字典的變量名來代替'i'。 – SethMMorton

+0

@KyleSponable:'target = thedictionaryinquestion [name]'我不確定你想用什麼字典。它是「高位命名」嗎?這是我看到的唯一字典。你的代碼看起來非常複雜,很少做。 –

相關問題