Alle Dateien aus dem Pythonkurs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

37 lines
796 B

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