Browse Source

Teile 10 bis 12.

develop
Raspithek 2 years ago
parent
commit
f7a5db9f39
  1. 6
      create_datetime.py
  2. 62
      pool.py
  3. BIN
      teil10/__pycache__/namen.cpython-39.pyc
  4. 5
      teil10/namen.csv
  5. 1
      teil10/namen.py
  6. 3
      teil10/namen.txt
  7. 29
      teil10/read_csv.py
  8. 17
      teil10/read_file.py
  9. 15
      teil10/write_file.py
  10. 12
      teil10/write_file_with.py
  11. 10
      teil11/create_datetime.py
  12. 0
      teil11/currentdate.py
  13. 0
      teil11/formatted_date.py
  14. 0
      teil11/zeitmessung.py
  15. 4004
      teil12/multi_write.log
  16. 32
      teil12/multi_write.py
  17. 46
      teil12/multithreading.py
  18. 52
      teil12/pool.py
  19. 10
      teil12/pool2.py
  20. 0
      teil12/pool3.py
  21. 55
      teil12/pool_squares.py
  22. 14
      teil12/pools.py
  23. 37
      teil12/race_condition.py
  24. 44
      teil12/race_condition_asyncio.py
  25. 43
      teil12/race_condition_lock.py
  26. 37
      teil12/threadpools.py
  27. 2000
      teil12/zahlen.txt

6
create_datetime.py

@ -1,6 +0,0 @@
from datetime import datetime
datum = datetime.strptime('12.12.2002 05:45', '%d.%m.%Y %M:%H')
print(datum)

62
pool.py

@ -1,62 +0,0 @@
#Quelle: https://stackoverflow.com/questions/6976372/mulitprocess-pools-with-different-functions
import datetime
import multiprocessing
import time
import random
from multiprocessing import Pool
def square(x):
# calculate the square of the value of x
print(x, x*x)
return x*x
def pf1(*args, **kwargs):
sleep_time = random.randint(3, 6)
print("Process : %s\tFunction : %s\tArgs: %s\tsleeping for %d\tTime : %s\n" % (multiprocessing.current_process().name, "pf1", args, sleep_time, datetime.datetime.now()))
print("Keyword Args from pf1: %s" % kwargs)
time.sleep(sleep_time)
print(multiprocessing.current_process().name, "\tpf1 done at %s\n" % datetime.datetime.now())
return (sum(*args), kwargs)
def pf2(*args):
sleep_time = random.randint(7, 10)
print("Process : %s\tFunction : %s\tArgs: %s\tsleeping for %d\tTime : %s\n" % (multiprocessing.current_process().name, "pf2", args, sleep_time, datetime.datetime.now()))
time.sleep(sleep_time)
print(multiprocessing.current_process().name, "\tpf2 done at %s\n" % datetime.datetime.now())
return sum(*args)
def pf3(*args):
sleep_time = random.randint(0, 3)
print("Process : %s\tFunction : %s\tArgs: %s\tsleeping for %d\tTime : %s\n" % (multiprocessing.current_process().name, "pf3", args, sleep_time, datetime.datetime.now()))
time.sleep(sleep_time)
print(multiprocessing.current_process().name, "\tpf3 done at %s\n" % datetime.datetime.now())
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__':
# Define the dataset
dataset = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
# Output the dataset
print ('Dataset: ' + str(dataset))
# Run this with a pool of 5 agents having a chunksize of 3 until finished
agents = 5
chunksize = 3
with Pool(processes=agents) as pool:
result = pool.map(square, dataset)
print("Result of Squares : %s\n\n" % result)
with Pool(processes=3) as pool:
result = pool.starmap(smap, [(pf1, [1,2,3], {'a':123, 'b':456}), (pf2, [11,22,33]), (pf3, [111,222,333])])

BIN
teil10/__pycache__/namen.cpython-39.pyc

Binary file not shown.

5
teil10/namen.csv

@ -0,0 +1,5 @@
Skinner,Seymour
Largo,Dewey
Krabappel,Edna
Hoover,Elisabeth
Chalmers,Gary
1 Skinner Seymour
2 Largo Dewey
3 Krabappel Edna
4 Hoover Elisabeth
5 Chalmers Gary

1
teil10/namen.py

@ -0,0 +1 @@
namen = ["Homer", "Marge", "Bart"]

3
teil10/namen.txt

@ -0,0 +1,3 @@
Homer
Marge
Bart

29
teil10/read_csv.py

@ -0,0 +1,29 @@
school = []
class Lehrer():
def __init__(self, vorname, name):
self.vorname = vorname
self.name = name
def __str__(self):
return f'{self.vorname} {self.name}'
try:
with open('namen.csv','r') as f:
for line in f:
splitted = line.strip().split(',')
name = splitted[0]
vorname = splitted[1]
lehrer = Lehrer(vorname,name)
school.append(lehrer)
except IOError as x:
print(f'I/O-Fehler: {x}')
print(school)
print(school[0])
print(school[3])

17
teil10/read_file.py

@ -0,0 +1,17 @@
namen = []
try:
f = open('namen.txt','r')
#f = open('namen.txt','r',encoding='utf-8')
for line in f:
namen.append(line)
# namen.append(line.strip())
except IOError as x:
print(f'I/O-Fehler: {x}')
finally:
if f != None:
f.close()
print(namen)

15
teil10/write_file.py

@ -0,0 +1,15 @@
from namen import namen
f = open('namen.txt','w')
try:
for name in namen:
f.write(f'{name}\n')
except IOError as err:
print(f'I/O-Fehler {err}')
finally:
if f != None:
f.close()

12
teil10/write_file_with.py

@ -0,0 +1,12 @@
from namen import namen
try:
with open('namen.txt','w') as f:
for name in namen:
f.write(f'{name}\n')
except IOError as err:
print(f'I/O-Fehler {err}')

10
teil11/create_datetime.py

@ -0,0 +1,10 @@
from datetime import datetime
datum = datetime.strptime('12.12.2002', '%d.%m.%Y')
print(f'Jahr: {datum.year}')
print(f'Monat: {datum.month}')
print(f'Tag: {datum.day}')
print(datum)

0
currentdate.py → teil11/currentdate.py

0
formatted_date.py → teil11/formatted_date.py

0
zeitmessung.py → teil11/zeitmessung.py

4004
teil12/multi_write.log

File diff suppressed because it is too large

32
teil12/multi_write.py

@ -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

@ -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

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

10
pool2.py → teil12/pool2.py

@ -7,10 +7,6 @@ if __name__ == '__main__':
job_list = [range(10000000)]*6
print(f'{job_list}')
if 'p' in sys.argv:
p = Pool(2)
print("Parallel map")
print(p.map(sum, job_list))
else:
print("Sequential map")
print(map(sum, job_list))
p = Pool(2)
print("Parallel map")
print(p.map(sum, job_list))

0
pool3.py → teil12/pool3.py

55
teil12/pool_squares.py

@ -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

@ -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

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

44
teil12/race_condition_asyncio.py

@ -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())

43
teil12/race_condition_lock.py

@ -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

@ -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

File diff suppressed because it is too large
Loading…
Cancel
Save