123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- """
- This module represents the Consumer.
- Computer Systems Architecture Course
- Assignment 1
- March 2020
- """
- from threading import Thread
- import time
- import threading
- class Consumer(Thread):
- """
- Class that represents a consumer.
- """
- def __init__(self, carts, marketplace, retry_wait_time, **kwargs):
- """
- Constructor.
- :type carts: List
- :param carts: a list of add and remove operations
- :type marketplace: Marketplace
- :param marketplace: a reference to the marketplace
- :type retry_wait_time: Time
- :param retry_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.carts = carts
- self.marketplace = marketplace
- self.retry_wait_time = retry_wait_time
- def run(self):
- name = threading.currentThread().getName()
- for cart in self.carts:
- cart_id = self.marketplace.new_cart()
- for operation in cart:
- if operation["type"] == "add":
- for _ in range(operation["quantity"]):
- while not self.marketplace.add_to_cart(cart_id, operation["product"]):
- time.sleep(self.retry_wait_time)
- elif operation["type"] == "remove":
- for _ in range(operation["quantity"]):
- self.marketplace.remove_from_cart(cart_id, operation["product"])
- products = []
- products.append(self.marketplace.place_order(cart_id))
- for product in products:
- for item in product:
- print(name + " bought " + str(item))
|