Raspithek
2 years ago
27 changed files with 6459 additions and 75 deletions
@ -1,6 +0,0 @@ |
|||||
from datetime import datetime |
|
||||
|
|
||||
datum = datetime.strptime('12.12.2002 05:45', '%d.%m.%Y %M:%H') |
|
||||
|
|
||||
print(datum) |
|
||||
|
|
@ -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])]) |
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1 @@ |
|||||
|
namen = ["Homer", "Marge", "Bart"] |
@ -0,0 +1,3 @@ |
|||||
|
Homer |
||||
|
Marge |
||||
|
Bart |
@ -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]) |
@ -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) |
@ -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() |
||||
|
|
||||
|
|
||||
|
|
@ -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}') |
||||
|
|
||||
|
|
||||
|
|
@ -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) |
||||
|
|
File diff suppressed because it is too large
@ -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() |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
@ -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') |
@ -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') |
||||
|
|
||||
|
|
@ -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') |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
@ -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") |
||||
|
|
@ -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}') |
@ -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()) |
@ -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}') |
@ -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() |
File diff suppressed because it is too large
Loading…
Reference in new issue