Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Coffee Factory: A multiple producer - multiple consumer approach
- Generate a base class Coffee which knows only the coffee name
- Create the Espresso, Americano and Cappuccino classes which inherit the base class knowing that
- each coffee type has a predetermined size.
- Each of these classes have a get message method
- Create 3 additional classes as following:
- * Distributor - A shared space where the producers puts coffees and the consumers takes them
- * CoffeeFactory - An infinite loop, which always sends coffees to the distributor
- * User - Another infinite loop, which always takes coffees from the distributor
- The scope of this exercise is to correctly use threads, classes and synchronization objects.
- The size of the coffee (ex. small, medium, large) is chosen randomly everytime.
- The coffee type is chosen randomly everytime.
- Example of output:
- Consumer 65 consumed espresso
- Factory 7 produced a nice small espresso
- Consumer 87 consumed cappuccino
- Factory 9 produced an italian medium cappuccino
- Consumer 90 consumed americano
- Consumer 84 consumed espresso
- Factory 8 produced a strong medium americano
- Consumer 135 consumed cappuccino
- Consumer 94 consumed americano
- """
- from threading import Semaphore, Thread
- class Coffee:
- """ Base class """
- def __init__(self, name, size):
- self.name = name
- self.size = size
- def get_name(self):
- return self.name
- def get_size(self):
- return self.size
- class Espresso(Coffee):
- """ Espresso implementation """
- def __init__(self, name, size):
- Coffee.__init__(self)
- self.size = size
- self.name = 'Espresso'
- def get_message(self):
- print('Your Espresso is ready')
- class Americano(Coffee):
- """ Espresso implementation """
- def __init__(self, name, size):
- Coffee.__init__(self)
- self.size = size
- self.name = 'Americano'
- def get_message(self):
- """ Output message """
- print('Your Americano is ready')
- class Cappuccino(Coffee):
- """ Espresso implementation """
- def __init__(self, name, size):
- Coffee.__init__(self)
- self.size = size
- self.name = 'Cappuccino'
- def get_message(self):
- """ Output message """
- print('Your Cappuccino is ready')
- class CoffeeFactory(Thread):
- def __init__(self, index, distributor):
- Thread.__init__(self)
- self.index = index
- self.distributor = distributor
- def run(self):
- while True:
- self.distributor.gol.acquire()
- print("Factory " + str(self.index) + " produced a Cappuccino")
- self.distributor.plin.release()
- class User(Thread):
- def __init__(self, index, distributor):
- Thread.__init__(self)
- self.index = index
- self.distributor = distributor
- def run(self):
- while True:
- self.distributor.plin.acquire()
- print("Consumer " + str(self.index) + " consumed a Cappuccino")
- self.distributor.gol.release()
- class Distributor:
- def __init__(self, gol, plin):
- self.gol = gol
- self.plin = plin
- def main():
- producers = []
- consumers = []
- gol = Semaphore(value = 10)
- plin = Semaphore(value = 0)
- for i in range(10):
- producers.append(CoffeeFactory(i, Distributor(gol, plin)))
- for i in range(10):
- consumers.append(User(i, Distributor(gol, plin)))
- for i in range(10):
- producers[i].start()
- consumers[i].start()
- for i in range(10):
- prodecers[i].join()
- consumers[i].join()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement