AttributeError:'int'对象没有属性'getattr'

时间:2013-08-01 02:45:05

标签: python dictionary multiprocessing attributeerror

我正在尝试让worker_manager工作,但我一直收到这个错误:

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)   

1 个答案:

答案 0 :(得分: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的函数可以获得动态定义的属性,但是整数没有任何动态属性,因此不清楚你要做什么。

相关问题