-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.py
47 lines (38 loc) · 1.4 KB
/
producer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
This module represents the Producer.
Computer Systems Architecture Course
Assignment 1
March 2021
"""
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)
self.products = products
self.marketplace = marketplace
self.republish_wait_time = republish_wait_time
self.kwargs = kwargs
self._id = self.marketplace.register_producer()
def run(self):
while self.marketplace.is_running:
for product in self.products:
for index in range(product[1]):
self.marketplace.publish(self._id, product[0])
time.sleep(product[2])
time.sleep(self.republish_wait_time)