Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Name
- Class
- Project 10
- Date
- File: producerconsumer.py
- Producer-consumer demo with no 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.
- However, on some runs, the producer and consumer may access the
- data out of order.
- """
- import time, random
- from threading import Thread, current_thread, 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("\n%s setting data to %d" % \
- (current_thread().name, 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("\n%s accessing data %d" % \
- (current_thread().name, 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, sleepMax, number_of_cons):
- """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
- self.number_of_consumers = number_of_cons
- def run(self):
- """"Announce start-up, sleep and write to shared
- cell the given number of times, and announce
- completion."""
- print("\n%s starting up " % self.name)
- for count in range(self.accessCount):
- consumer_count = 0
- time.sleep(random.randint(1, self.sleepMax))
- while consumer_count <= self.number_of_consumers:
- self.cell.setData(count + 1)
- consumer_count = consumer_count+1
- print("\n%s is done producing" % self.getName())
- class Consumer(Thread):
- """A consumer of data in a shared cell."""
- global conCount
- conCount = 0
- def __init__(self, cell, accessCount, sleepMax):
- """Create a consumer with the given shared cell,
- number of accesses, and maximum sleep interval."""
- global conCount
- conCount = conCount+1
- Thread.__init__(self, name = "Consumer_"+str(conCount))
- self.accessCount = accessCount
- self.cell = cell
- self.sleepMax = sleepMax
- self.value = None
- def run(self):
- """Announce start-up, sleep and write to shared
- cell the given number of times, and announce
- completion."""
- print("\n%s starting up" % self.name)
- for count in range(self.accessCount):
- time.sleep(random.randint(1, self.sleepMax))
- while self.value == self.cell.getData:
- time.sleep(random.randint(1, self.sleepMax))
- self.value = self.cell.getData()
- print("\n%s is done consuming" % self.name)
- def main():
- accessCount = int(input("Enter the number of accesses: "))
- conCount = int(input("Enter the number of consumers: "))
- sleepMax = 4
- cell = SharedCell()
- consumers = []
- p = Producer(cell, accessCount, sleepMax, conCount)
- consumers = []
- for consumer in range(conCount):
- consumers.append(Consumer(cell, accessCount, sleepMax))
- print("Starting the threads")
- for consumer in consumers:
- print(consumer.name)
- p.start()
- for consumer in consumers:
- consumer.start()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement