Untitled
unknown
python
2 years ago
1.5 kB
4
Indexable
from random import choice, randint from string import ascii_lowercase from threading import Condition, Lock, Thread from time import sleep class Writer(Thread): SLEEP_TIME = (1, 3) WRITE_TIME = 1 def __init__(self, name): super().__init__() self.name = name def write(self): global book, notifier, records book.acquire() print(f"Writer {self.name} started rewriting") records = "" for _ in range(5): with notifier: records += choice(ascii_lowercase) notifier.notify_all() sleep(Writer.WRITE_TIME) book.release() def run(self): while True: print(f"Writer {self.name} are resting") sleep(randint(*Writer.SLEEP_TIME)) print(f"Writer {self.name} wanna work") self.write() class Reader(Thread): def __init__(self, name): super().__init__() self.name = name def run(self): global notifier, records while True: with notifier: notifier.wait() print(f"Reader {self.name} reads {records}") if __name__ == "__main__": NUM_WRITERS = 2 book = Lock() notifier = Condition(Lock()) records = "" WRITERS = [Writer(str(num)) for num in range(NUM_WRITERS)] READERS = [Reader(str(num))for num in range(NUM_WRITERS)] for i in enumerate(WRITERS): WRITERS[i[0]].start() READERS[i[0]].start() sleep(1)
Editor is loading...