python - How to use .join in this process -


hi creating worker generator class, getting hangs on terminate know need use .join close them can't figure out how pass process name terminate function. thought somehow save global variable, thinking dictionary. when time have workers terminated have terminate access function variable , terminate applicable process(es) after dropping poison pill in message queue

class worker_manager:       = test_imports()       #somevarforp        #somevarforp2         def generate(control_queue, threadname, runnum):           if threadname == 'one':               print ("starting import_1 number %d") % runnum               p = multiprocessing.process(target=i.import_1, args=(control_queue, runnum))               #somevarforp = p               p.start()                   if threadname == 'two':                print ("starting import_2 number %d") % runnum               p = multiprocessing.process(target=i.import_2, args=(control_queue, runnum))               #somevarforp2 = p2               p.start()           if threadname == 'three':                   p = multiprocessing.process(target=i.import_1, args=(control_queue, runnum))               print ("starting import_1 number %d") % runnum               p2 = multiprocessing.process(target=i.import_2, args=(control_queue, runnum))               print ("starting import_2 number %d") % runnum               #somevarforp = p               #somevarforp2 = p2               p.start()               p2.start()        def terminate(threadname):            if threadname == 'one':                #self.somevarforp.join()            if threadname == 'two':                #self.somefarforp2.join()            if threadname == 'three':                #self.somevarforp.join()                #self.somevarforp2.join() 

any ideas guys , gals have appreciated not stuck on specific way , kind of new python suggestions welcome!

edit: complete code

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)         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)      

edit worker_manager bit out of going used. rest of develop heads up.

first, i'm not sure understand join does, if you're using function called terminate. when call join, make parent process wait child process finish. still need tell child process needs finish (e.g., posting on queue, or sending signal), or you'll end waiting forever.

second, fact you've left self parameters out of methods, used old-style class, created class attribute wanted instance attribute, , started talking global variables seems suited instance attribute implies may have misunderstandings classes well.

but big problem seems want map string names instance attributes (or other kind of variable). while can this, you never want to. instead of having separate attribute each name, use single dict, value each name. example:

class worker_manager:     # ...     def __init__(self):         self.children = {}     def generate(self, control_queue, threadname, runnum):         if threadname == 'one':             print ("starting import_1 number %d") % runnum             p = multiprocessing.process(target=i.import_1, args=(control_queue, runnum))             self.children[threadname]             p.start()         # ...     def terminate(self, threadname):         self.children[threadname].join() 

you can clean further factoring out common parts of if blocks. example, might want dispatch table. and, while we're @ it, let's use more standard names, 4-character indentation instead of random, new-style class, etc.:

class workermanager(object):     # ...     names = {'one': 'import_1', 'two': 'import_2', 'three': 'import_3'}     def __init__(self):         self.children = {}     def generate(self, control_queue, threadname, runnum):         name = workermanager.names[threadname]         target = i.getattr(name)         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() 

there's lot more refactored if see more of code, should enough started.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -