# L-12 MCS 507 Mon 18 Sep 2023 : classproducer

"""
Illustration of producer/consumer with threads.
An object of the class Producer is a thread that
will append to a queue consecutive integers in
a given range and at a given pace.
"""

import threading
from random import randint
from time import sleep

class Producer(threading.Thread):
    """
    Appends integers to a queue.
    """
    def __init__(self, t, q, a, b, p):
        """
        Thread t to add integers in [a, b] to q,
        sleeping between 1 and p seconds.
        """
        threading.Thread.__init__(self, name=t)
        self.queue = q
        self.begin = a
        self.end = b
        self.pace = p

    def run(self):
        """
        Produces integers at some pace.
        """
        print(self.getName() + " starts...")
        for i in range(self.begin, self.end+1):
            nbr = randint(1, self.pace)
            print(self.getName() + \
                " sleeps %d seconds" % nbr)
            sleep(nbr)
            print("appending %d to queue" % i)
            self.queue.append(i)
        print("production terminated")
