UNI_Python/_crashkurs/thread.py
2024-02-29 22:31:32 +01:00

51 lines
830 B
Python

import random as rnd
import threading
import time
import matplotlib.pyplot as plt
def racer(number, seed):
rnd.seed(seed)
start = time.time()
duration = rnd.randint(1, 10)
time.sleep(duration)
end = time.time()
results[number] = f"Racer {number} in {end - start}"
n = 20
threads = []
results = {}
total_start = time.time()
for car in range(n):
rand = rnd.random()
threads.append(threading.Thread(target=racer, args=(car, rand)))
threads[car].start()
for car in range(n):
threads[car].join()
fig, ax = plt.subplots()
"""
ax.plot(
[car for car in range(n)],
[results[car] for car in range(n)]
)
"""
ax.bar(
[car for car in range(n)],
[results[car] for car in range(n)]
)
ax.set(
xlabel="Racer",
ylabel="Zeit",
title="Rennen",
)
ax.grid(True)
plt.show()