Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. """
  2. Coffee Factory: A multiple producer - multiple consumer approach
  3.  
  4. Generate a base class Coffee which knows only the coffee name
  5. Create the Espresso, Americano and Cappuccino classes which inherit the base class knowing that
  6. each coffee type has a predetermined size.
  7. Each of these classes have a get message method
  8.  
  9. Create 3 additional classes as following:
  10. * Distributor - A shared space where the producers puts coffees and the consumers takes them
  11. * CoffeeFactory - An infinite loop, which always sends coffees to the distributor
  12. * User - Another infinite loop, which always takes coffees from the distributor
  13.  
  14. The scope of this exercise is to correctly use threads, classes and synchronization objects.
  15. The size of the coffee (ex. small, medium, large) is chosen randomly everytime.
  16. The coffee type is chosen randomly everytime.
  17.  
  18. Example of output:
  19.  
  20. Consumer 65 consumed espresso
  21. Factory 7 produced a nice small espresso
  22. Consumer 87 consumed cappuccino
  23. Factory 9 produced an italian medium cappuccino
  24. Consumer 90 consumed americano
  25. Consumer 84 consumed espresso
  26. Factory 8 produced a strong medium americano
  27. Consumer 135 consumed cappuccino
  28. Consumer 94 consumed americano
  29. """
  30. from threading import Semaphore, Thread
  31.  
  32. class Coffee:
  33. """ Base class """
  34. def __init__(self, name, size):
  35. self.name = name
  36. self.size = size
  37.  
  38. def get_name(self):
  39. return self.name
  40.  
  41. def get_size(self):
  42. return self.size
  43.  
  44.  
  45. class Espresso(Coffee):
  46. """ Espresso implementation """
  47. def __init__(self, name, size):
  48. Coffee.__init__(self)
  49. self.size = size
  50. self.name = 'Espresso'
  51.  
  52. def get_message(self):
  53. print('Your Espresso is ready')
  54.  
  55. class Americano(Coffee):
  56. """ Espresso implementation """
  57. def __init__(self, name, size):
  58. Coffee.__init__(self)
  59. self.size = size
  60. self.name = 'Americano'
  61.  
  62. def get_message(self):
  63. """ Output message """
  64. print('Your Americano is ready')
  65.  
  66. class Cappuccino(Coffee):
  67. """ Espresso implementation """
  68. def __init__(self, name, size):
  69. Coffee.__init__(self)
  70. self.size = size
  71. self.name = 'Cappuccino'
  72.  
  73. def get_message(self):
  74. """ Output message """
  75. print('Your Cappuccino is ready')
  76.  
  77.  
  78. class CoffeeFactory(Thread):
  79. def __init__(self, index, distributor):
  80. Thread.__init__(self)
  81. self.index = index
  82. self.distributor = distributor
  83.  
  84. def run(self):
  85. while True:
  86. self.distributor.gol.acquire()
  87. print("Factory " + str(self.index) + " produced a Cappuccino")
  88. self.distributor.plin.release()
  89.  
  90. class User(Thread):
  91. def __init__(self, index, distributor):
  92. Thread.__init__(self)
  93. self.index = index
  94. self.distributor = distributor
  95.  
  96. def run(self):
  97. while True:
  98. self.distributor.plin.acquire()
  99. print("Consumer " + str(self.index) + " consumed a Cappuccino")
  100. self.distributor.gol.release()
  101.  
  102.  
  103. class Distributor:
  104. def __init__(self, gol, plin):
  105. self.gol = gol
  106. self.plin = plin
  107.  
  108. def main():
  109.  
  110. producers = []
  111. consumers = []
  112. gol = Semaphore(value = 10)
  113. plin = Semaphore(value = 0)
  114.  
  115. for i in range(10):
  116. producers.append(CoffeeFactory(i, Distributor(gol, plin)))
  117.  
  118. for i in range(10):
  119. consumers.append(User(i, Distributor(gol, plin)))
  120.  
  121. for i in range(10):
  122. producers[i].start()
  123. consumers[i].start()
  124.  
  125. for i in range(10):
  126. prodecers[i].join()
  127. consumers[i].join()
  128.  
  129. if __name__ == '__main__':
  130. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement