Write a simulation program for the fruit market. The farmer will be able to
produce different types of fruits (apple, orange, grape, and watermelon),
and put them in the market to sell. The market has limited capacity and
farmers have to stand in a queue if the capacity is exceeded to sell their
fruits. Consumers can come to the market any time and purchase their
desired fruits; and if the fruits they want to buy runs out, they are willing to
wait until the supply of that kind is ready. (Hint: implementing this market
will encounter the producer and consumer problem, and it probably needs
multiple buffers for different kinds of fruits).
from threading import Thread
import time
import random
from Queue import Queue
wait = Queue(4)
class Producer(Thread):
def display(M):
N = {apple, orange, grape,watermelon}
global wait
while True:
number= random.choice(N)
wait.add(number)
print("Produced", number)
time.sleep(random.random())
class Consumer(Thread):
def display(M):
global wait
while True:
number = wait.get()
wait.complete()
print("Consumed", number)
time.sleep(random.random())
Producer().start()
Consumer().start()
Comments
Leave a comment