Question
Python II Redo the producer/consumer program so that it allows multiple consumers. Each consumer must be able to consume the same data before the producer
Python II
Redo the producer/consumer program so that it allows multiple consumers. Each consumer must be able to consume the same data before the producer produces more data. Below it the code to redo.
import time, random from threading import Thread, currentThread, Condition
class SharedCell(object): """Shared data that sequences writing before reading.""" def __init__(self): """Can produce but not consume at startup.""" self.data = -1 self.writeable = True self.condition = Condition()
def setData(self, data): """Second caller must wait until someone has consumed the data before resetting it.""" self.condition.acquire() while not self.writeable: self.condition.wait() print("%s setting data to %d" % \ (currentThread().getName(), data)) self.data = data self.writeable = False self.condition.notify() self.condition.release()
def getData(self): """Caller must wait until someone has produced the data before accessing it.""" self.condition.acquire() while self.writeable: self.condition.wait() print("%s accessing data %d" % \ (currentThread().getName(), self.data)) self.writeable = True self.condition.notify() self.condition.release() return self.data
class Producer(Thread): """A producer of data in a shared cell."""
def __init__(self, cell, accessCount, sleepInterval): Thread.__init__(self, name = "Producer") self.accessCount = accessCount self.cell = cell self.sleepInterval = sleepInterval
def run(self): """Resets the data in the cell and goes to sleep, the given number of times.""" print("%s starting up" % self.getName()) for count in range(self.accessCount): time.sleep(random.randint(1, self.sleepInterval)) self.cell.setData(count + 1) print("%s is done producing " % self.getName())
class Consumer(Thread): """A consumer of data in a shared cell."""
def __init__(self, cell, accessCount, sleepInterval): Thread.__init__(self, name = "Consumer") self.accessCount = accessCount self.cell = cell self.sleepInterval = sleepInterval
def run(self): """Accesses the data in the cell and goes to sleep, the given number of times.""" print("%s starting up " % self.getName()) for count in range(self.accessCount): time.sleep(random.randint(1, self.sleepInterval)) value = self.cell.getData() print("%s is done consuming " % self.getName())
def main(): accessCount = int(input("Enter the number of accesses: ")) cell = SharedCell() p = Producer(cell, accessCount, 4) c = Consumer(cell, accessCount, 4) print("Starting the threads") p.start() c.start()
if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started