繁体   English   中英

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

[英]AttributeError: 'int' object has no attribute 'getattr'

我正在尝试使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)   
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的函数可获取动态定义的属性,但是整数无论如何都没有任何动态属性,因此不清楚您要做什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM