Answer to Question #253496 in Python for shishi

Question #253496

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).


1
Expert's answer
2021-10-20T16:38:48-0400
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS