-
Notifications
You must be signed in to change notification settings - Fork 1
/
notification.py
58 lines (48 loc) · 1.61 KB
/
notification.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
import threading
import logging
from queue import Queue, Full
class Message:
def __init__(self, user, name, path, count, type=None):
self.user = user
self.name = name
self.path = path
self.count = count
if type is None:
type = ['mail', ]
self.type = type
class Notification:
def __init__(self):
self.message = None
self.__event = threading.Event()
self.__cond = threading.Condition()
self.__mail_queue = Queue(100)
def _send_mail(self):
while not self.__event.is_set():
message = self.__mail_queue.get()
# TODO
def send_mail(self):
threading.Thread(target=self._send_mail, name='send_mail_real').satrt()
while not self.__event.is_set():
with self.__cond:
self.__cond.wait()
if 'mail' in self.message.type:
try:
self.__mail_queue.put(self.message, timeout=1)
except Full:
logging.error('main queue is full')
def send_sms(self):
while not self.__event.is_set():
with self.__cond:
self.__cond.wait()
# TODO send sms
def notify(self, message):
with self.__cond:
self.message = message
self.__cond.notify_all()
def start(self):
mail = threading.Thread(target=self.send_mail, name='send_mail')
mail.start()
sms = threading.Thread(target=self.send_sms, name='send_sms')
sms.start()
def stop(self):
self.__event.set()