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

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