#encoding: UTF-8 #Quelle: https://stackoverflow.com/questions/6976372/mulitprocess-pools-with-different-functions import datetime from timeit import default_timer as timer import multiprocessing import time import random from multiprocessing import Pool __verbose__ = False def print_verbose(s): if __verbose__: print(s) def calc_sum_square(zahlen, *args,**kwargs): start = kwargs['start'] ende = kwargs['end'] threadnr = kwargs['threadnr'] summe = 0 # print_verbose(f'x={x}') # time.sleep(1.0) # print_verbose(f'kwargs={kwargs}') print_verbose(f'von {start} bis {ende}') for n in range(start,ende): i = zahlen[n] print_verbose(f'Berechnung:{threadnr}: {i}, {i*i}') summe = summe + i*i return summe def smap(f, *arg): args, kwargs = arg # print(f'smap() args= {args}') time.sleep(1.0) # print(f'smap() kwargs={kwargs}') time.sleep(1.0) return f(list(args), **kwargs) if __name__ == '__main__': start = timer() zahlen = [i for i in range(10000000)] ende = timer() print(f'Zahlenliste erzeugt in {ende-start}s') split = int(len(zahlen) / 3) print(f'split={split}') time.sleep(1.0) start = timer() with Pool(processes=3) as pool: result = pool.starmap(smap, [(calc_sum_square, zahlen, {'threadnr':1,'start':0, 'end':split}), (calc_sum_square, zahlen, {'threadnr':2,'start':split+1,'end':split*2}), (calc_sum_square, zahlen,{'threadnr':3,'start':(split*2)+1,'end':split*3})]) print(f'Ergebnis: {result}') ende = timer() print(f'Ausführzeit: {ende-start}s')