Teile 10 bis 12.

This commit is contained in:
2023-03-28 12:26:50 +02:00
parent 298151634d
commit f7a5db9f39
27 changed files with 6459 additions and 75 deletions

55
teil12/pool_squares.py Normal file
View File

@@ -0,0 +1,55 @@
#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_square(x, *args,**kwargs):
start = kwargs['start']
ende = kwargs['end']
threadnr = kwargs['threadnr']
summe = 0
print_verbose(f'args={args}')
time.sleep(1.0)
print_verbose(f'kwargs={kwargs}')
print_verbose(f'von {start} bis {ende}')
for n in range(start,ende):
print_verbose(f'Berechnung:{threadnr}: {n}, {n*n}')
summe = summe + n*n
return summe
def smap(f, *arg):
args, kwargs = arg
return f(list(args), **kwargs)
if __name__ == '__main__':
zahlen = [i for i in range(10000000)]
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_square, zahlen, {'threadnr':1,'start':0, 'end':split}), (calc_square, zahlen, {'threadnr':2,'start':split+1,'end':split*2}), (calc_square, zahlen,{'threadnr':3,'start':(split*2)+1,'end':split*3})])
print(f'Ergebnis: {result}')
ende = timer()
print(f'Ausführzeit: {ende-start}s')