Why won't my python else statement trigger? -
here code, these dummy classes replaced more useful. want while loop pull data queue see if poison pill dropped. if not want whatever in else statement trigger. reason wait till gets poison pill , execute kill condition if statement
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 == true: alive = control_queue.get() count = count + 1 if alive == 't1kill':#<==will trigger print ("killing thread type 1 number %d") % thread_number run = false else:#<== won't trigger print ("thread type 1 number %d run count %d") % (thread_number, count)
if needed rest of code is:
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 == true: alive = control_queue.get() count = count + 1 if alive == 't1kill': print ("killing thread type 1 number %d") % thread_number run = false else: print ("thread type 1 number %d run count %d") % (thread_number, count) def import_2(self, control_queue, thread_number): print ("import_2 number %d started") % thread_number run = true count = 1 while run == true: alive = control_queue.get() count = count + 1 if alive == 't2kill': print ("killing thread type 2 number %d") % thread_number run = false else: print ("thread type 2 number %d run count %d") % (thread_number, count) class worker_manager: def __init__(self): self.children = {} def generate(self, control_queue, threadname, runnum): = test_imports() 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 p.start() elif threadname == 'two': print ("starting import_2 number %d") % runnum p = multiprocessing.process(target=i.import_2, args=(control_queue, runnum)) self.children[threadname] = p p.start() elif 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 self.children[threadname] = p self.children[threadname] = p2 p.start() p2.start() else: print ("not valid choice choose 1 2 or three") 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): manager.generate(control, threadname, i) thread_count = thread_count + 1 time.sleep(runnum)#let threads thing print ("terminating threads") in range(thread_count): control.put("t1kill") control.put("t2kill") manager.terminate(threadname)
please note import_2
identical import_1
except prints different. point prove ability handle different thread types.
in driver code, control.put("t1kill")
first.
and handler t1kill
sets run = false
, won't come through while run == true
loop ever again.
so, there's no opportunity else
triggered.
if want test it, add put
s dummy value:
for in range(thread_count): control.put("dummy") control.put("t1kill") control.put("t2kill")
however, in real code, you're going want manager.generate
method put useful values on queue.
as side note, you're making code more complicated needs be.
first, it's bad idea write while run == true:
instead of while run:
. pep 8's programming recommendations section says:
don't compare boolean values true or false using ==.
but really, can return
you're done, , scrap run
flag entirely:
while true: alive = control_queue.get() count = count + 1 if alive == 't1kill': print ("killing thread type 1 number %d") % thread_number return else: print ("thread type 1 number %d run count %d") % (thread_number, count)
(there people tell break
, return
, etc. "bad structured programming". python isn't c.)
Comments
Post a Comment