-
Notifications
You must be signed in to change notification settings - Fork 0
/
kons.py
59 lines (54 loc) · 1.65 KB
/
kons.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
# Wyłączenie programu
import sys
import signal
# Przetrzywanie zasobu na chwilę
import time
# Logowanie informacji
from Logger import logger as myLogger
# Implementacja Rozproszonego Monitora
from Distr_Monit import DistributedMonitor
# End of program - sigint
def signal_handler(sig, frame):
myLogger.info("You pressed Ctrl+C! End of program.")
# Zakończ pracę
dMonitor.end_work()
sys.exit(0)
class Product():
def __init__(self):
self.buffer = []
self.counter = 0
def add(self, num):
self.buffer.append(num)
self.counter +=1
def rm(self):
self.counter -=1
return self.buffer.pop(0)
def exists(self):
return bool(self.counter)
# Funkcja main.
if __name__ == "__main__":
# Init rozproszonego monitora
dMonitor = DistributedMonitor(sys.argv[2], sys.argv[1], sys.argv[2:], Product())
# Handler zakończenia CTRL+C
signal.signal(signal.SIGINT, signal_handler)
i = 0
while True:
# Zdobądź dostęp do zasobu
produkt = dMonitor.acquire()
# Modyfikuj zasób
if produkt.exists():
i = 0
myLogger.info("Konsumuje produkt: " + str(produkt.rm()) + ".")
else:
i+=1
myLogger.info("Nie ma nic już " + str(i) + " raz.")
# Przetrzymaj przez chwilę zasób
time.sleep(1)
# Zwolnij zasób
dMonitor.release(produkt)
# Jeżeli 3 razy nic się nie pojawiło do skonsumowania to wychodzimy
if i >=3:
myLogger.info("Nie ma tutaj nic dla mnie 3 razy z rzędu.")
# Zakończ pracę
dMonitor.end_work()
sys.exit(0)