From f815de7a278cfb94cbb2e440250be75a6cd7be54 Mon Sep 17 00:00:00 2001 From: TimWeaving Date: Fri, 3 Nov 2023 17:05:55 +0000 Subject: [PATCH] Parallel processing class template --- symmer/process_handler.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 symmer/process_handler.py diff --git a/symmer/process_handler.py b/symmer/process_handler.py new file mode 100644 index 00000000..87499731 --- /dev/null +++ b/symmer/process_handler.py @@ -0,0 +1,35 @@ +import multiprocessing as mp +import ray + +class ProcessHandler: + + method = 'mp' + + def __init__(self): + self.n_logical_cores = mp.cpu_count() + + def _process_ray(self, func): + def worker(iter, shared=None): + return func(iter, shared) + return worker + + def _process_mp(self, func): + def worker(iter, shared=None): + return func(iter, shared) + return worker + + def parallelize(self, func): + if self.method == 'mp': + return self._process_mp(func) + elif self.method == 'ray': + return self._process_ray(func) + +if __name__ == '__main__': + PH = ProcessHandler() + + @PH.parallelize + def add_n(l, n): + return [i+n for i in l] + + print(add_n([1,2,3,4,5,6,7], 3)) +