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

4004
teil12/multi_write.log Normal file

File diff suppressed because it is too large Load Diff

32
teil12/multi_write.py Normal file
View File

@@ -0,0 +1,32 @@
import threading
import logging
logging.basicConfig(
format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s',
filename='multi_write.log',
level=logging.DEBUG)
datei = open('zahlen.txt','w')
def write_number(zahl):
for i in range(0,1000):
logging.debug(f'Thread {zahl}/schreibe {i}')
datei.write(f'{i}:{zahl}\n')
datei.flush()
thread_1 = threading.Thread(target=write_number,args=(1,))
thread_2 = threading.Thread(target=write_number,args=(2,))
logging.debug('starte Thread 1')
thread_1.start()
logging.debug('starte Thread 2')
thread_2.start()
thread_1.join()
thread_2.join()

46
teil12/multithreading.py Normal file
View File

@@ -0,0 +1,46 @@
from timeit import default_timer as timer
import time
import threading
def calc_square(numbers, verbose=False):
for n in range(1,numbers):
q= n*n
if verbose:
print(f'\n{n} ^ 2 = {q}')
time.sleep(0.1)
def calc_cube(numbers,verbose=False):
for n in range(1,numbers):
k = n*n*n
if verbose:
print(f'\n{n} ^ 3 = {k}')
time.sleep(0.1)
start = timer()
thread_square = threading.Thread(target=calc_square, args=(100,True))
thread_cube = threading.Thread(target=calc_cube, args=(100,True))
thread_cube.start()
thread_square.start()
thread_cube.join()
thread_square.join()
ende = timer()
differenz_mit_print = ende - start
print(f'Zeit mit print():{differenz_mit_print}s')
start = timer()
thread_square = threading.Thread(target=calc_square, args=(100,False))
thread_cube = threading.Thread(target=calc_cube, args=(100,False))
thread_cube.start()
thread_square.start()
thread_cube.join()
thread_square.join()
ende = timer()
differenz_ohne_print = ende - start
print(f'Zeit ohne print():{differenz_ohne_print}s')

52
teil12/pool.py Normal file
View File

@@ -0,0 +1,52 @@
# encoding: UTF-8
from timeit import default_timer as timer
import datetime
import multiprocessing
import time
import random
from multiprocessing import Pool
def func1(*args, **kwargs):
sleep_time = random.randint(3, 6)
print(f'Prozess : {multiprocessing.current_process().name}\tFunktion : func1\tArgs:{args}\tgeschlafen {sleep_time} \tZeit : {datetime.datetime.now()}\n')
print("Keyword Args from func1: %s" % kwargs)
time.sleep(sleep_time)
print(f'{multiprocessing.current_process().name}\t func1 fertig: {datetime.datetime.now()}\n')
return sum(*args)
def func2(*args):
sleep_time = random.randint(7, 10)
print(f'Prozess : {multiprocessing.current_process().name}\tFunktion : func2\tArgs:{args}\tgeschlafen {sleep_time} \tZeit : {datetime.datetime.now()}\n')
time.sleep(sleep_time)
print(f'{multiprocessing.current_process().name}\t func2 fertig: {datetime.datetime.now()}\n')
return sum(*args)
def func3(*args):
sleep_time = random.randint(0, 3)
print(f'Prozess : {multiprocessing.current_process().name}\tFunktion : func3\ttArgs:{args}\tgeschlafen {sleep_time} \tZeit : {datetime.datetime.now()}\n')
time.sleep(sleep_time)
print(f'{multiprocessing.current_process().name}\t func3 fertig: {datetime.datetime.now()}\n')
return sum(*args)
def smap(f, *arg):
if len(arg) == 2:
args, kwargs = arg
return f(list(args), **kwargs)
elif len(arg) == 1:
args = arg
return f(*args)
if __name__ == '__main__':
start = timer()
with Pool(processes=3) as pool:
result = pool.starmap(smap, [(func1, [1,2,3],{'a':123, 'b':456}), (func2, [11,22,33]), (func3, [111,222,333])])
print(f'Ergebnis: {result}')
ende = timer()
diff = ende - start
print(f'Ausführzeit: {diff}s')

12
teil12/pool2.py Normal file
View File

@@ -0,0 +1,12 @@
# Quelle: https://stackoverflow.com/questions/35528093/multithreading-in-python-with-a-threadpool
from multiprocessing import Pool
import sys
if __name__ == '__main__':
job_list = [range(10000000)]*6
print(f'{job_list}')
p = Pool(2)
print("Parallel map")
print(p.map(sum, job_list))

20
teil12/pool3.py Normal file
View File

@@ -0,0 +1,20 @@
# Quelle: https://www.codesdope.com/blog/article/multiprocessing-using-pool-in-python/
import time
from multiprocessing import Pool
def square(x):
print(f"start process:{x}")
square = x * x
print(f"square {x}:{square}")
time.sleep(1)
print(f"end process:{x}")
if __name__ == "__main__":
starttime = time.time()
pool = Pool()
pool.map(square, range(0, 5))
pool.close()
endtime = time.time()
print(f"Time taken {endtime-starttime} seconds")

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')

14
teil12/pools.py Normal file
View File

@@ -0,0 +1,14 @@
from multiprocessing import Pool
import time
def calculate_square(n):
return n*n
if __name__ == "__main__":
starttime = time.time()
pool = Pool()
pool.map(calculate_square, range(0, 5))
pool.close()
endtime = time.time()
print(f"Time taken {endtime-starttime} seconds")

37
teil12/race_condition.py Normal file
View File

@@ -0,0 +1,37 @@
import threading
import time
import random
gesamtwert = 0
message = ''
def calculate_sum(thread_nummer):
global gesamtwert
global message
for i in range(100 + thread_nummer):
zwischen = gesamtwert
#Zufällige Wartezeit zwischen 0,1s und 0,5s
wartezeit = random.randint(1,5+thread_nummer)
time.sleep(0.1 * wartezeit)
zwischen += 1
gesamtwert = zwischen
message = f'Thread {thread_nummer} fertig'
print(f'Thread + {thread_nummer} abgearbeitet.')
# zwei Threads starten, um die Summe der Zahlen zu berechnen
gesamtwert = 0
t1 = threading.Thread(target=calculate_sum, args=(1,))
t2 = threading.Thread(target=calculate_sum, args=(2,))
t3 = threading.Thread(target=calculate_sum, args=(3,))
t1.start()
t2.start()
t3.start()
t1.join()
t3.join()
t2.join()
print(f'gesamtwert={gesamtwert}')
print(f'message={message}')

View File

@@ -0,0 +1,44 @@
import asyncio
import random
import time
gesamtwert = 0
message = ''
async def calculate_sum(lock, thread_nummer):
global gesamtwert
global message
for i in range(100 + thread_nummer):
async with lock:
zwischen = gesamtwert
#Zufällige Wartezeit zwischen 0,1s und 0,5s
wartezeit = random.randint(1,5+thread_nummer)
time.sleep(0.1 * wartezeit)
zwischen += 1
async with lock:
gesamtwert = zwischen
message = f'Thread {thread_nummer} fertig'
print(f'Thread + {thread_nummer} abgearbeitet.')
async def main():
#async Lock erzeugen
lock = asyncio.Lock()
# drei Threads starten, um die Summe der Zahlen zu berechnen
threads = [asyncio.create_task(calculate_sum(lock, _+1)) for _ in range(3)]
#Warten auf alle Threads
await asyncio.gather(*threads)
print(f'gesamtwert={gesamtwert}')
print(f'message={message}')
if __name__ == '__main__':
asyncio.run(main())

View File

@@ -0,0 +1,43 @@
import threading
import time
import random
gesamtwert = 0
message = ''
lock = threading.Lock()
def calculate_sum(thread_nummer,lock):
global gesamtwert
global message
for i in range(100 + thread_nummer):
with lock:
zwischen = gesamtwert
#Zufällige Wartezeit zwischen 0,1s und 0,5s
wartezeit = random.randint(1,5+thread_nummer)
time.sleep(0.1 * wartezeit)
zwischen += 1
with lock:
gesamtwert = zwischen
message = f'Thread {thread_nummer} fertig'
print(f'Thread + {thread_nummer} abgearbeitet.')
# zwei Threads starten, um die Summe der Zahlen zu berechnen
gesamtwert = 0
t1 = threading.Thread(target=calculate_sum, args=(1,lock,))
t2 = threading.Thread(target=calculate_sum, args=(2,lock,))
t3 = threading.Thread(target=calculate_sum, args=(3,lock,))
t1.start()
t2.start()
t3.start()
t1.join()
t3.join()
t2.join()
print(f'gesamtwert={gesamtwert}')
print(f'message={message}')

37
teil12/threadpools.py Normal file
View File

@@ -0,0 +1,37 @@
import concurrent.futures
import random
import time
gesamtwert = 0
message = ''
def calculate_sum(thread_nummer):
global gesamtwert
global message
for i in range(100 + thread_nummer):
zwischen = gesamtwert
#Zufällige Wartezeit zwischen 0,1s und 0,5s
wartezeit = random.randint(1,5+thread_nummer)
time.sleep(0.1 * wartezeit)
zwischen += 1
gesamtwert = zwischen
message = f'Thread {thread_nummer} fertig'
print(f'Thread + {thread_nummer} abgearbeitet.')
def main():
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
threads = [executor.submit(calculate_sum, i+1) for i in range(3)]
results = [t.result() for t in threads]
print(f'gesamtwert={gesamtwert}')
print(f'message={message}')
if __name__ == '__main__':
main()

2000
teil12/zahlen.txt Normal file

File diff suppressed because it is too large Load Diff