Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.0 kB
12
Indexable
Never
from multiprocessing import Process, Queue
from time import sleep
from string import ascii_lowercase
from random import choice

class Producter(Process):
    LETTERS = ascii_lowercase.replace("q", "")
    
    def __init__(self, queue: Queue, lenght=5):
        super().__init__()
        self.lenght = lenght
        self.queue = queue
        
    
    def run(self):
        queue = self.queue
        for _ in range(self.lenght):
            queue.put(choice(self.LETTERS))
        queue.put("q")


class Consumer(Process):
    def __init__(self, queue: Queue):
        super().__init__()
        self.queue = queue
    
    
    def run(self):
        queue = self.queue
        while True:
            msg = queue.get()
            print(f"I've got letter {msg}")
            if msg == "q":
                break

if __name__ == "__main__":
    queue = Queue()
    lenght = 5
    producter = Producter(queue, lenght)
    consumer = Consumer(queue)
    consumer.start()
    producter.start()
    sleep(1)