-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
69 lines (53 loc) · 1.68 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from random import randint, choice
from time import sleep
from config import config
from dictionary import get_random_quote
from mapping import Mapping
from disk import Raid
from threading import Thread, Lock
from rwlock import RWLock
mutex = Lock()
rwlock = RWLock()
def reader_loop(mapping, reader_no):
while True:
with rwlock.r_locked():
mutex.acquire()
key = randint(1, 10)
try:
val = mapping.get_value(key)
print(f'Reader {reader_no}: {val}')
sleep(1)
except KeyError:
print(f'Reader {reader_no}: Key {key} not found')
def writer_loop(mapping, writer_no):
while True:
with rwlock.w_locked():
mutex.acquire()
key = randint(1, 10)
val = get_random_quote()
mapping.set_value(key, val)
print(f'Wrote {writer_no}: {val}')
sleep(1)
def start_readers(readers_count, mapping):
for reader_no in range(readers_count):
print('Starting reader')
r = Thread(target=reader_loop, args=(mapping, reader_no))
r.start()
def start_writers(writers_count, mapping):
for writer_no in range(writers_count):
print('Starting writer')
w = Thread(target=writer_loop, args=(mapping, writer_no))
w.start()
def main():
try:
raid = Raid(config.disk_files)
mapping = Mapping(raid)
print(raid.disks[0].read(0))
print(raid)
start_writers(config.writers_count, mapping)
start_readers(config.readers_count, mapping)
finally:
print('Closing raid')
raid.close()
if __name__ == '__main__':
main()