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 ...@@ -2,6 +2,7 @@ from multiprocessing import Manager, Lock, Pool, cpu_count
from contextlib import closing from contextlib import closing
import sys import sys
class ListMultiprocessing(object): class ListMultiprocessing(object):
def __init__(self, function, n_cpu=None, verbose=True): def __init__(self, function, n_cpu=None, verbose=True):
super(ListMultiprocessing).__init__() super(ListMultiprocessing).__init__()
...@@ -11,43 +12,46 @@ class ListMultiprocessing(object): ...@@ -11,43 +12,46 @@ class ListMultiprocessing(object):
self.result = manager.list() self.result = manager.list()
self.count = manager.Value('i', 0) self.count = manager.Value('i', 0)
self.verbose = verbose self.verbose = verbose
def __segment__(self): def __segment__(self):
avg = len(self.array) / self.n_cpu avg = len(self.array) / self.n_cpu
last = 0.0 last = 0.0
while last < len(self.array): while last < len(self.array):
yield self.array[int(last):int(last + avg)] yield self.array[int(last) : int(last + avg)]
last += avg last += avg
def __compute__(self, samples): def __compute__(self, samples):
for sample in samples: for sample in samples:
result=self.function(sample) result = self.function(sample)
with Lock(): with Lock():
self.count.value +=1 self.count.value += 1
self.result.append(result) self.result.append(result)
if self.verbose: if self.verbose:
self.__progress__(f'Processing sample: {sample}') self.__progress__(f'Processing sample: {sample}')
def execute(self, array): def execute(self, array):
self.array = array self.array = array
self.total = len(array) self.total = len(array)
with closing(Pool(self.n_cpu, maxtasksperchild=1)) as pool: 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] [task.get() for task in tasks]
print('\nDone') print('\nDone')
def get_result(self): 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 bar_len = 40
filled_len = int(round(bar_len * self.count.value / float(self.total))) filled_len = int(round(bar_len * self.count.value / float(self.total)))
percents = round(100.0 * self.count.value / float(self.total), 1) percents = round(100.0 * self.count.value / float(self.total), 1)
bar = '█' * filled_len + '░' * (bar_len - filled_len) bar = '█' * filled_len + '░' * (bar_len - filled_len)
sys.stdout.write(f'\r|{bar}| {percents}% - {status} ') sys.stdout.write(f'\r|{bar}| {percents}% - {status} ')
sys.stdout.flush() sys.stdout.flush()
\ No newline at end of file
Markdown is supported
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