Aufräumarbeiten
This commit is contained in:
@@ -16,4 +16,6 @@ CC-BY-SA Olli Graf
|
||||
| 9 | Typkonvertierung|
|
||||
|10 | Dateien|
|
||||
|11 | Datum und Zeit|
|
||||
|12 | Multithreading|
|
||||
|12 | Netzwerk|
|
||||
|
||||
|
@@ -1,46 +0,0 @@
|
||||
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')
|
@@ -1,45 +0,0 @@
|
||||
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())
|
||||
|
Reference in New Issue
Block a user