**CENAGE PYTHON** Question: 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. The program should Print the message Consumer # starting up, where # is the number of the consumer starting from 0. The producer will set the data as many times as there are accesses, giving each consumer thread a chance to access the data each time. The producer should set the data to 1 initially, and then increment it for each additional access. After each consumer has consumed the data for each access, print the message Consumer # is done consuming, where # is the number of the consumer starting from 0. A sample program execution is shown below. Note that the order of the consumer start ups and accesses may vary with each program execution. Enter the number of consumers: 2 Enter the number of accesses: 3 Starting the threads Producer starting up Consumer 0 starting up Consumer 1 starting up Producer setting data to 1 Consumer 1 accessing data 1 Consumer 0 accessing data 1 Producer setting data to 2 Consumer 0 accessing data 2 Consumer 1 accessing data 2 Producer setting data to 3 Producer is done producing Consumer 0 accessing data 3 Consumer 0 is done consuming Consumer 1 accessing data 3 Consumer 1 is done consuming **Code Below** import time, random from threading import Thread, currentThread class SharedCell(object):     """Shared data for the producer/consumer problem."""          def __init__(self):         """Data undefined at startup."""         self.data = -1     def setData(self, data):         """Producer's method to write to shared data."""         print("%s setting data to %d" % \               (currentThread().getName(), data))         self.data = data     def getData(self):         """Consumer's method to read from shared data."""         print("%s accessing data %d" % \               (currentThread().getName(), self.data))         return self.data class Producer(Thread):     """A producer of data in a shared cell."""     def __init__(self, cell, accessCount, sleepMax):         """Create a producer with the given shared cell,         number of accesses, and maximum sleep interval."""         Thread.__init__(self, name = "Producer")         self.accessCount = accessCount         self.cell = cell         self.sleepMax = sleepMax     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.sleepMax))             self.cell.setData(count + 1)         print("%s is done producing\n" % self.getName()) class Consumer(Thread):     """A consumer of data in a shared cell."""     def __init__(self, cell, accessCount, sleepMax):         """Create a consumer with the given shared cell,         number of accesses, and maximum sleep interval."""         Thread.__init__(self, name = "Consumer")         self.accessCount = accessCount         self.cell = cell         self.sleepMax = sleepMax     def run(self):         """Announce start-up, sleep and write to shared         cell the given number of times, and announce         completion."""         print("%s starting up\n" % self.getName())         for count in range(self.accessCount):             time.sleep(random.randint(1, self.sleepMax))             value = self.cell.getData()         print("%s is done consuming\n" % self.getName()) def main():     accessCount = int(input("Enter the number of accesses: "))     sleepMax = 4     cell = SharedCell()     p = Producer(cell, accessCount, sleepMax)     c = Consumer(cell, accessCount, sleepMax)     print("Starting the threads")     p.start()     c.start() main()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Topic Video
Question

**CENAGE PYTHON** Question: 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.

The program should

  1. Print the message Consumer # starting up, where # is the number of the consumer starting from 0.
  2. The producer will set the data as many times as there are accesses, giving each consumer thread a chance to access the data each time. The producer should set the data to 1 initially, and then increment it for each additional access.
  3. After each consumer has consumed the data for each access, print the message Consumer # is done consuming, where # is the number of the consumer starting from 0.

A sample program execution is shown below.

Note that the order of the consumer start ups and accesses may vary with each program execution.

Enter the number of consumers: 2 Enter the number of accesses: 3 Starting the threads Producer starting up Consumer 0 starting up Consumer 1 starting up Producer setting data to 1 Consumer 1 accessing data 1 Consumer 0 accessing data 1 Producer setting data to 2 Consumer 0 accessing data 2 Consumer 1 accessing data 2 Producer setting data to 3 Producer is done producing Consumer 0 accessing data 3 Consumer 0 is done consuming Consumer 1 accessing data 3 Consumer 1 is done consuming

**Code Below**

import time, random
from threading import Thread, currentThread

class SharedCell(object):
    """Shared data for the producer/consumer problem."""
    
    def __init__(self):
        """Data undefined at startup."""
        self.data = -1

    def setData(self, data):
        """Producer's method to write to shared data."""
        print("%s setting data to %d" % \
              (currentThread().getName(), data))
        self.data = data

    def getData(self):
        """Consumer's method to read from shared data."""
        print("%s accessing data %d" % \
              (currentThread().getName(), self.data))
        return self.data

class Producer(Thread):
    """A producer of data in a shared cell."""

    def __init__(self, cell, accessCount, sleepMax):
        """Create a producer with the given shared cell,
        number of accesses, and maximum sleep interval."""
        Thread.__init__(self, name = "Producer")
        self.accessCount = accessCount
        self.cell = cell
        self.sleepMax = sleepMax

    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.sleepMax))
            self.cell.setData(count + 1)
        print("%s is done producing\n" % self.getName())

class Consumer(Thread):
    """A consumer of data in a shared cell."""

    def __init__(self, cell, accessCount, sleepMax):
        """Create a consumer with the given shared cell,
        number of accesses, and maximum sleep interval."""
        Thread.__init__(self, name = "Consumer")
        self.accessCount = accessCount
        self.cell = cell
        self.sleepMax = sleepMax

    def run(self):
        """Announce start-up, sleep and write to shared
        cell the given number of times, and announce
        completion."""
        print("%s starting up\n" % self.getName())
        for count in range(self.accessCount):
            time.sleep(random.randint(1, self.sleepMax))
            value = self.cell.getData()
        print("%s is done consuming\n" % self.getName())

def main():
    accessCount = int(input("Enter the number of accesses: "))
    sleepMax = 4
    cell = SharedCell()
    p = Producer(cell, accessCount, sleepMax)
    c = Consumer(cell, accessCount, sleepMax)
    print("Starting the threads")
    p.start()
    c.start()

main()
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education