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