|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
from sys import platform |
|
6
|
|
|
from tqdm import tqdm |
|
7
|
|
|
|
|
8
|
|
|
from ._process import _process_ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
if platform.startswith("linux"): |
|
12
|
|
|
initializer = tqdm.set_lock |
|
13
|
|
|
initargs = (tqdm.get_lock(),) |
|
14
|
|
|
else: |
|
15
|
|
|
initializer = None |
|
16
|
|
|
initargs = () |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def proxy(args): |
|
20
|
|
|
return _process_(*args) |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def single_process(process_func, process_infos): |
|
24
|
|
|
return [process_func(info) for info in process_infos] |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
def multiprocessing_wrapper(process_func, process_infos, n_processes): |
|
28
|
|
|
import multiprocessing as mp |
|
29
|
|
|
|
|
30
|
|
|
process_infos = tuple(process_infos) |
|
31
|
|
|
|
|
32
|
|
|
print("\n process_infos ", process_infos) |
|
33
|
|
|
|
|
34
|
|
|
with mp.Pool(n_processes, initializer=initializer, initargs=initargs) as pool: |
|
35
|
|
|
return pool.map(process_func, process_infos) |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def pathos_wrapper(process_func, search_processes_paras, n_processes): |
|
39
|
|
|
import pathos.multiprocessing as pmp |
|
40
|
|
|
|
|
41
|
|
|
with pmp.Pool(n_processes, initializer=initializer, initargs=initargs) as pool: |
|
42
|
|
|
return pool.map(process_func, search_processes_paras) |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def joblib_wrapper(process_func, search_processes_paras, n_processes): |
|
46
|
|
|
from joblib import Parallel, delayed |
|
47
|
|
|
|
|
48
|
|
|
jobs = [delayed(process_func)(*info_dict) for info_dict in search_processes_paras] |
|
49
|
|
|
return Parallel(n_jobs=n_processes)(jobs) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
dist_dict = { |
|
53
|
|
|
"joblib": (joblib_wrapper, _process_), |
|
54
|
|
|
"multiprocessing": (multiprocessing_wrapper, proxy), |
|
55
|
|
|
"pathos": (pathos_wrapper, proxy), |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def _get_distribution(distribution): |
|
60
|
|
|
if hasattr(distribution, "__call__"): |
|
61
|
|
|
return (distribution, _process_), {} |
|
62
|
|
|
|
|
63
|
|
|
elif isinstance(distribution, dict): |
|
64
|
|
|
dist_key = list(distribution.keys())[0] |
|
65
|
|
|
dist_paras = list(distribution.values())[0] |
|
66
|
|
|
|
|
67
|
|
|
return dist_dict[dist_key], dist_paras |
|
68
|
|
|
|
|
69
|
|
|
elif isinstance(distribution, str): |
|
70
|
|
|
return dist_dict[distribution], {} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
def run_search(searches, distribution, n_processes): |
|
74
|
|
|
if n_processes == "auto": |
|
75
|
|
|
n_processes = len(searches) |
|
76
|
|
|
|
|
77
|
|
|
searches_tuple = [(search,) for search in searches] |
|
78
|
|
|
|
|
79
|
|
|
if n_processes == 1: |
|
80
|
|
|
results_list = single_process(_process_, searches) |
|
81
|
|
|
else: |
|
82
|
|
|
(distribution, process_func), dist_paras = _get_distribution(distribution) |
|
83
|
|
|
|
|
84
|
|
|
results_list = distribution(process_func, searches_tuple, n_processes) |
|
85
|
|
|
|
|
86
|
|
|
return results_list |
|
87
|
|
|
|