python - AttributeError: 'int' object has no attribute 'getattr' -
i trying worker_manager work, keep getting error:
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' here code working with, worker_manager, part going live. supposed take thread names dictionary , access associated classes. advice anyone? thanks!
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 throwing 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 number: ")) threadnum = int(raw_input("enter number of threads: ")) threadname = raw_input("enter number: ") thread_count = 0 print ("starting threads") 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 thing print ("terminating threads") 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 throwing error i here not defined in local scope. means it's defined globally. means variable referring 1 defined here:
for in range(threadnum): if intentional, it's bad practice. try avoid using global variables. also, that's integer. trying do:
i.getattr(name) on integer. supposed do? there function called getattr dynamically defined attributes, integers don't have dynamic attributes anyway, it's unclear trying do.
Comments
Post a Comment