Skip to content

Instantly share code, notes, and snippets.

@evz
Created September 26, 2017 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evz/b1b241ff5c3db8d54b5b4f1d4da5ce50 to your computer and use it in GitHub Desktop.
Save evz/b1b241ff5c3db8d54b5b4f1d4da5ce50 to your computer and use it in GitHub Desktop.
import multiprocessing
import time
class ChildThread(multiprocessing.Process):
def __init__(self, stopper, counter, **kwargs):
super().__init__(**kwargs)
self.stopper = stopper
self.counter = counter
def run(self):
time.sleep(self.counter)
print('I am a child {}'.format(self.counter))
class ParentThread(multiprocessing.Process):
def __init__(self, stopper, **kwargs):
super().__init__(**kwargs)
self.stopper = stopper
def run(self):
counter = 0
while not self.stopper.is_set():
counter += 1
child_stopper = threading.Event()
child = ChildThread(child_stopper, counter)
child.start()
exited = child.join(timeout=5)
if not exited:
print("It won't DIEEEE {}".format(counter))
child.terminate()
print(child.exitcode, 'exitcode')
if __name__ == "__main__":
import sys
import time
import threading
stopper = threading.Event()
parent = ParentThread(stopper)
def signalHandler(signum, frame):
stopper.set()
parent.join()
parent.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment