-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15f183e
commit f815de7
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
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
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)) | ||
|