0

我正在尝试让 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)   
4

1 回答 1

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

于 2013-08-01T02:52:51.597 回答