Advertisement
TorroesPrime

Untitled

Aug 3rd, 2022
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. import time, random
  2. from threading import Thread, currentThread
  3.  
  4. class SharedCell(object):
  5.     """shared data for the producers/consumer problem."""
  6.    
  7.     def __init__(self):
  8.         self.data = -1
  9.        
  10.     def setData(self, data):
  11.         """producers method to write to shared data."""
  12.         print("%s setting data to "%d %(currentThread().getName(),data))
  13.         self.data = data
  14.  
  15.     def getData(self):
  16.         """consumer's method to read from shared data."""
  17.         print("%s accessing data %d"%(currentThread().getName(),self.data))
  18.         return self.data
  19.    
  20. class Producer(Thread):
  21.     """Represents a producer."""
  22.  
  23.     def ___init__(self, cell, accessCount, sleepMax):
  24.         """creates a producer with the given shared cell, number of accesses, and maximum sleep interval."""
  25.         super().__init__(self,name="Producer")
  26.         self.accessCount = accessCount
  27.         self.cell = cell
  28.         self.sleepMax = sleepMax
  29.  
  30.     def run(self):
  31.         """announce start-up, sleep, and write to shared cell the given number of times, and announces completion."""
  32.         print("%s starting up"%self.getName())
  33.         for count in range(self.accessCount):
  34.             time.sleep(random.randint(1,self.sleepMax))
  35.             self.cell.setData(count+1)
  36.         print("%s is done producing"%self.getName())
  37.  
  38. class Consumer(Thread):
  39.     """Represents a consumer"""
  40.     conCount = 0
  41.  
  42.     def __init__(self, cell, accessCount,sleepMax):
  43.         """Create a producer with a given shared cell, number of accesses, and maximum sleep interval."""
  44.         conCount = conCount+1
  45.         Thread.__init__(self, name = "Consumer_"+str(conCount))
  46.         self.accessCount = accessCount
  47.         self.cell = cell
  48.         self.sleepMax = sleepMax
  49.        
  50.  
  51.     def run(self):
  52.         """Announces start-up, sleep, and read from shared cell the given number of times, and announce completion."""
  53.         print("%s starting up "%self.getName())
  54.         for count in range(self.accessCount):
  55.             time.sleep(random.randint(1,self.sleepMax))
  56.             value = self.cell.getData()
  57.         print("%s is done consuming"%self.getName())
  58. def main():
  59.     """get the number of accesses from the user, create a shared cell, and create and startup a producer and consumer"""
  60.     accessCount = int(input("Enter the number of accesses:"))
  61.     sleepMax = 4
  62.     cell = SharedCell()
  63.     producer = Producer(cell, accessCount, sleepMax)
  64.     consumer = Consumer(cell, accessCount, sleepMax)
  65.     print("Starting the threads")
  66.     producer.start()
  67.     consumer.start()
  68. main()
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement