From 6ecb951dba79eaa51d3a68a048bfa8ef16c465a9 Mon Sep 17 00:00:00 2001 From: Thomas Letzgus <thomas.letzgus@student.reutlingen-university.de> Date: Thu, 25 May 2023 18:34:19 +0000 Subject: [PATCH] Upload New File --- consumer.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 consumer.py diff --git a/consumer.py b/consumer.py new file mode 100644 index 0000000..2827253 --- /dev/null +++ b/consumer.py @@ -0,0 +1,33 @@ +from buffer import Buffer +import threading +import random +import time + + +class Consumer: + def __init__(self, buffer_obj): + self.buffer = buffer_obj + + def consume(self): + while True: + time.sleep(random.uniform(0.5, 2.0)) # Random interval between checks + + with self.buffer.condition: + while self.buffer.empty(): + self.buffer.condition.wait() # Wait until buffer is not empty + + car = self.buffer.pop() + print("Consumer: Car", car.get_car_id(), "parked out") + + if self.buffer.full(): + # Buffer was completely full before the withdrawal, wake up all sleeping producers + self.buffer.condition.notify_all() + + +# Example usage: +buffer = Buffer(size=10) # Assuming buffer is already initialized with a size of 10 +consumer = Consumer(buffer) + +# Create and start the consumer thread +consumer_thread = threading.Thread(target=consumer.consume) +consumer_thread.start() -- GitLab