From a1cf3d6e3b78753abbe4202266714931fdecb599 Mon Sep 17 00:00:00 2001 From: Thomas Letzgus <thomas.letzgus@student.reutlingen-university.de> Date: Thu, 25 May 2023 18:34:41 +0000 Subject: [PATCH] Upload New File --- producer.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 producer.py diff --git a/producer.py b/producer.py new file mode 100644 index 0000000..4c3cf2d --- /dev/null +++ b/producer.py @@ -0,0 +1,37 @@ +from car import Car +from buffer import Buffer +import threading +import random +import time + + +class Producer: + def __init__(self, buffer_obj): + self.buffer = buffer_obj + + def produce(self): + while True: + time.sleep(random.uniform(0.5, 2.0)) # Random interval between checks + + with self.buffer.condition: + while self.buffer.full(): + self.buffer.condition.wait() # Wait until buffer is not full + + car_id = random.randint(1, 100) # Generate a random car ID + car = Car(car_id) + self.buffer.push(car) + print("Producer: Car", car.get_car_id(), "parked") + + if self.buffer.empty(): + # Buffer was empty before parking, wake up all sleeping consumers + self.buffer.condition.notify_all() + + +# Example usage: +buffer = Buffer(size=10) # Assuming buffer is already initialized with a size of 10 +producer = Producer(buffer) + +# Create and start the producer thread +producer_thread = threading.Thread(target=producer.produce) +producer_thread.start() + -- GitLab