Created
September 26, 2017 20:38
-
-
Save evz/b1b241ff5c3db8d54b5b4f1d4da5ce50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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