Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use .join in this process
    primarykey
    data
    text
    <p>Hi I am creating this worker generator class, I am getting hangs on terminate I know I need to use a <code>.join</code> to close them but I can't figure out how to pass the process name to the <code>terminate</code> function. My thought was somehow save it to a global variable, I was thinking a dictionary. Then when it is time to have the workers terminated have <code>terminate</code> access that function variable and terminate the applicable process(es) after dropping a poison pill in the message queue</p> <pre><code>class worker_manager: i = 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() </code></pre> <p>Any ideas you guys and gals have would be appreciated I am not stuck on any specific way to do this and I am kind of new to python so any suggestions are welcome!</p> <p>Edit: complete code</p> <pre><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 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) </code></pre> <p>Edit only <code>worker_manager</code> is the only bit out of this that is actually going to be used. The rest of this is just to develop it just a heads up. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload