From 97f15576fbeb9584883d3a34663b2adc59069481 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Tue, 28 Jul 2020 15:07:36 -0700 Subject: [PATCH] [sonic-py-common] Add task_base.py (#5049) Add new task_base.py module from sonic-daemon-base into sonic-py-common package --- .../sonic_py_common/task_base.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/sonic-py-common/sonic_py_common/task_base.py diff --git a/src/sonic-py-common/sonic_py_common/task_base.py b/src/sonic-py-common/sonic_py_common/task_base.py new file mode 100644 index 0000000000..e1738ffba2 --- /dev/null +++ b/src/sonic-py-common/sonic_py_common/task_base.py @@ -0,0 +1,50 @@ +import multiprocessing +import os +import signal +import threading + + +# +# ProcessTaskBase ===================================================================== +# +class ProcessTaskBase(object): # TODO: put this class to swss-platform-common + def __init__(self): + self.task_process = None + self.task_stopping_event = multiprocessing.Event() + + def task_worker(self): + pass + + def task_run(self): + if self.task_stopping_event.is_set(): + return + + self.task_process = multiprocessing.Process(target=self.task_worker) + self.task_process.start() + + def task_stop(self): + self.task_stopping_event.set() + os.kill(self.task_process.pid, signal.SIGKILL) + + +# +# ThreadTaskBase ===================================================================== +# +class ThreadTaskBase(object): # TODO: put this class to swss-platform-common; + def __init__(self): + self.task_thread = None + self.task_stopping_event = threading.Event() + + def task_worker(self): + pass + + def task_run(self): + if self.task_stopping_event.is_set(): + return + + self.task_thread = threading.Thread(target=self.task_worker) + self.task_thread.start() + + def task_stop(self): + self.task_stopping_event.set() + self.task_thread.join()