123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- """
- This module represents the Producer.
- Computer Systems Architecture Course
- Assignment 1
- March 2020
- """
- from threading import Thread
- import time
- class Producer(Thread):
- """
- Class that represents a producer.
- """
- def __init__(self, products, marketplace, republish_wait_time, **kwargs):
- """
- Constructor.
- @type products: List()
- @param products: a list of products that the producer will produce
- @type marketplace: Marketplace
- @param marketplace: a reference to the marketplace
- @type republish_wait_time: Time
- @param republish_wait_time: the number of seconds that a producer must
- wait until the marketplace becomes available
- @type kwargs:
- @param kwargs: other arguments that are passed to the Thread's __init__()
- """
- Thread.__init__(self, **kwargs)
- self.products = products
- self.marketplace = marketplace
- self.republish_wait_time = republish_wait_time
- def run(self):
- producer_id = self.marketplace.register_producer()
- while True:
- for product in self.products:
- new_products = []
- for _ in range(product[1]):
- new_products.append(product[0])
- for new_product in new_products:
- while not self.marketplace.publish(producer_id, new_product):
- time.sleep(self.republish_wait_time)
- time.sleep(product[2])
|