Python Redo the producer/consumer program so that it allows multiple consumers.
ID: 3713117 • Letter: P
Question
Python
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. This is the consumer program I was given to work with and I am not sure what to do. Any help would be very much appreciated. Thank you.
"""
File: producerconsumer2.py
Producer-consumer demo with synchronization.
Producer and consumer both access shared data a given number
of times. They sleep a random interval before each access.
The data must be produced before it is consumed, and be produced
and consumed just once.
The condition and Boolean flag on the shared data guarantee that
the producer and consumer access the data in the correct order.
"""
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()
main()
Explanation / Answer
Here is changed synchronized version of producerconsumer. You only need to know how many consumers are just wait before that many consumers have consumed data instead of checking wrtitable True/False. I have removed writable condition
Output:
Enter the number of Objects to be produced: 5
Enter the number of consumers: 2
Starting the threads
Producer starting up
Consumer1 starting up
Consumer2 starting up
Producer setting data to 1
Consumer1 accessing data 1
Consumer2 accessing data 1
Producer setting data to 2
Consumer1 accessing data 2
Consumer2 accessing data 2
Producer setting data to 3
Consumer1 accessing data 3
Consumer2 accessing data 3
Producer setting data to 4
Consumer1 accessing data 4
Consumer2 accessing data 4
Producer setting data to 5
Producer is done producing
Consumer1 accessing data 5
Consumer1 is done consuming data
Consumer2 accessing data 5
Consumer2 is done consuming data
Process finished with exit code 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.