Skip to content

Commit

Permalink
Parallel processing class template
Browse files Browse the repository at this point in the history
  • Loading branch information
TimWeaving committed Nov 3, 2023
1 parent 15f183e commit f815de7
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions symmer/process_handler.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit f815de7

Please sign in to comment.