python - Please explain this early termination of multiprocess.Process subclass? -


i have following test code subclassing process multiprocessing:

import multiprocessing mp  class testprocess(mp.process):     def __init__(self,name):         super().__init__()         self.name = name      def run(self):         while true:             print('{} says "i alive"'.format(self.name))  if __name__ == "__main__" :      procs = [testprocess(i) in list('abc')]      try:         p in procs:             print('starting {}'.format(p.name))             p.start()     except keyboardinterrupt:         print('caught interrupt')     except exception e:         print(str(e))     finally:         p in procs:             print('stopping {}'.format(p.name))             p.terminate() 

for reason terminates without ever sending ctrl + c , process b , c never seem run:

host:~ user$ python process_example.py     starting     starting b     starting c     says "i alive"     [... x 16 lines]     says "i alistopping     stopping b     stopping c 

if can explain this, i'd appreciate it.

update

thanks prompt responses; i've updated following:

import time import multiprocessing mp  class testprocess(mp.process):     def __init__(self,name):         super().__init__()         self.name = name      def run(self):         while true:             print('{} says "i alive"'.format(self.name))             time.sleep(1)  if __name__ == "__main__" :      procs = [testprocess(i) in list('abc')]      try:         p in procs :             print('starting {}'.format(p.name))             p.start()         p in procs :             print('joining {}'.format(p.name))             p.join()     except keyboardinterrupt:         print('caught interrupt')     except exception e:         print(str(e))     finally:         p in procs :             print('stopping {}'.format(p.name))             p.terminate() 

now get:

host:~ user$ python process_example.py starting starting b starting c says "i alive" joining b says "i alive" c says "i alive" says "i alive" b says "i alive" c says "i alive" says "i alive" b says "i alive" c says "i alive" says "i alive" b says "i alive" c says "i alive" says "i alive" b says "i alive" c says "i alive" b says "i alive" says "i alive" c says "i alive" ^ccaught interrupt stopping stopping b stopping c 

so joining 1 thread necessary?

it's unclear me expect do. after starting processes, code enters finally: block , explicitly terminates of them. of course stop.

as why "a" says it's alive, may vary across platforms (os). run() method "busy loop", printing fast possible non-stop. processes killed after starting them looks like, on your platform, os never got around scheduling time slice other processes before terminated them.

try, e.g., sticking time.sleep(10) after loop starts processes. delay killing processes 10 seconds, , that's enough time b , c love ;-) operating system before they're killed.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)