Commit f66b733a authored by Alessia Marcolini's avatar Alessia Marcolini
Browse files

Black formatting

parent dd49a005
......@@ -2,6 +2,7 @@ from multiprocessing import Manager, Lock, Pool, cpu_count
from contextlib import closing
import sys
class ListMultiprocessing(object):
def __init__(self, function, n_cpu=None, verbose=True):
super(ListMultiprocessing).__init__()
......@@ -11,43 +12,46 @@ class ListMultiprocessing(object):
self.result = manager.list()
self.count = manager.Value('i', 0)
self.verbose = verbose
def __segment__(self):
avg = len(self.array) / self.n_cpu
last = 0.0
while last < len(self.array):
yield self.array[int(last):int(last + avg)]
yield self.array[int(last) : int(last + avg)]
last += avg
def __compute__(self, samples):
for sample in samples:
result=self.function(sample)
result = self.function(sample)
with Lock():
self.count.value +=1
self.count.value += 1
self.result.append(result)
if self.verbose:
self.__progress__(f'Processing sample: {sample}')
def execute(self, array):
self.array = array
self.total = len(array)
with closing(Pool(self.n_cpu, maxtasksperchild=1)) as pool:
tasks = [pool.apply_async(self.__compute__, args=(part,)) for part in self.__segment__()]
tasks = [
pool.apply_async(self.__compute__, args=(part,))
for part in self.__segment__()
]
[task.get() for task in tasks]
print('\nDone')
def get_result(self):
return([x for x in self.result])
return [x for x in self.result]
def __progress__(self, status): # count, total, status=''):
def __progress__(self, status):#count, total, status=''):
bar_len = 40
filled_len = int(round(bar_len * self.count.value / float(self.total)))
percents = round(100.0 * self.count.value / float(self.total), 1)
bar = '█' * filled_len + '░' * (bar_len - filled_len)
sys.stdout.write(f'\r|{bar}| {percents}% - {status} ')
sys.stdout.flush()
\ No newline at end of file
sys.stdout.flush()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment