-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel.py
83 lines (54 loc) · 1.96 KB
/
parallel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
"""
Created on Sun May 24 17:41:28 2020
@author: Didier Vega-Oliveros
"""
import multiprocessing as mp
import math
def worker_job(lock, listFeat, task_queue, result_list, function, argsF):
proc_name = mp.current_process().name
while True:
try:
next_task = task_queue.get_nowait()
except:
print(str(proc_name)+': Exiting- Queue empty! ',end="")
break
else:
idxFeat = next_task
if len(idxFeat) <= 0:
print('-------------------------------------------------')
continue
lFeat = [listFeat[x] for x in idxFeat]
print(str(proc_name)+' : '+str(idxFeat[0])+'-'+str(idxFeat[-1]))
resps = function(lFeat, argsF)
if(len(resps) > 0):
with lock:
result_list.extend(resps)
return True
def For(listFeat, function, argsF):
num_workers = mp.cpu_count()
numPJob = math.ceil(len(listFeat)/float(num_workers))
mgr = mp.Manager()
task_queue = mgr.Queue()
result_list = mgr.list()
lock = mgr.Lock()
idxBL = list()
for idxB in range(len(listFeat)):
idxBL.append(idxB)
if len(idxBL) >= numPJob:
task_queue.put(idxBL)
idxBL = list()
if len(idxBL) > 0:
task_queue.put(idxBL)
# Start consumers
processes = []
print ('Creating '+str(num_workers)+ ' workers processing ',numPJob,' jobs')
for w in range(num_workers):
p = mp.Process(target=worker_job, args=(lock, listFeat, task_queue, result_list, function, argsF))
processes.append(p)
p.start()
for w in processes:
w.join()
return list(result_list)
if __name__ == "__main__":
print('main')