mirror of
https://github.com/Mayuri-Chan/pyrofork.git
synced 2026-01-06 15:14:52 +00:00
Add Event Handler (for single updates)
This commit is contained in:
parent
bd75dc8082
commit
07c3d96d82
1 changed files with 37 additions and 3 deletions
|
|
@ -103,6 +103,7 @@ class Client:
|
||||||
|
|
||||||
INVITE_LINK_RE = re.compile(r"^(?:https?://)?t\.me/joinchat/(.+)$")
|
INVITE_LINK_RE = re.compile(r"^(?:https?://)?t\.me/joinchat/(.+)$")
|
||||||
DIALOGS_AT_ONCE = 100
|
DIALOGS_AT_ONCE = 100
|
||||||
|
UPDATE_WORKERS = 2
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
session_name: str,
|
session_name: str,
|
||||||
|
|
@ -144,6 +145,7 @@ class Client:
|
||||||
self.is_idle = Event()
|
self.is_idle = Event()
|
||||||
|
|
||||||
self.update_queue = Queue()
|
self.update_queue = Queue()
|
||||||
|
self.event_queue = Queue()
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Use this method to start the Client after creating it.
|
"""Use this method to start the Client after creating it.
|
||||||
|
|
@ -176,9 +178,12 @@ class Client:
|
||||||
self.rnd_id = self.session.msg_id
|
self.rnd_id = self.session.msg_id
|
||||||
self.get_dialogs()
|
self.get_dialogs()
|
||||||
|
|
||||||
for i in range(self.workers):
|
for i in range(self.UPDATE_WORKERS):
|
||||||
Thread(target=self.update_worker, name="UpdateWorker#{}".format(i + 1)).start()
|
Thread(target=self.update_worker, name="UpdateWorker#{}".format(i + 1)).start()
|
||||||
|
|
||||||
|
for i in range(self.workers):
|
||||||
|
Thread(target=self.event_worker, name="EventWorker#{}".format(i + 1)).start()
|
||||||
|
|
||||||
mimetypes.init()
|
mimetypes.init()
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
|
@ -187,9 +192,12 @@ class Client:
|
||||||
"""
|
"""
|
||||||
self.session.stop()
|
self.session.stop()
|
||||||
|
|
||||||
for i in range(self.workers):
|
for i in range(self.UPDATE_WORKERS):
|
||||||
self.update_queue.put(None)
|
self.update_queue.put(None)
|
||||||
|
|
||||||
|
for i in range(self.workers):
|
||||||
|
self.event_queue.put(None)
|
||||||
|
|
||||||
def update_worker(self):
|
def update_worker(self):
|
||||||
name = threading.current_thread().name
|
name = threading.current_thread().name
|
||||||
log.debug("{} started".format(name))
|
log.debug("{} started".format(name))
|
||||||
|
|
@ -201,7 +209,33 @@ class Client:
|
||||||
break
|
break
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.update_handler(self, update)
|
# TODO: Fetch users and chats
|
||||||
|
if isinstance(update, (types.Update, types.UpdatesCombined)):
|
||||||
|
for i in update.updates:
|
||||||
|
self.event_queue.put(i)
|
||||||
|
elif isinstance(update, (types.UpdateShortMessage, types.UpdateShortChatMessage)):
|
||||||
|
self.event_queue.put(update)
|
||||||
|
elif isinstance(update, types.UpdateShort):
|
||||||
|
self.event_queue.put(update.update)
|
||||||
|
else:
|
||||||
|
print(">>>>>", type(update))
|
||||||
|
except Exception as e:
|
||||||
|
log.error(e, exc_info=True)
|
||||||
|
|
||||||
|
log.debug("{} stopped".format(name))
|
||||||
|
|
||||||
|
def event_worker(self):
|
||||||
|
name = threading.current_thread().name
|
||||||
|
log.debug("{} started".format(name))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
event = self.event_queue.get()
|
||||||
|
|
||||||
|
if event is None:
|
||||||
|
break
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.update_handler(self, event)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(e, exc_info=True)
|
log.error(e, exc_info=True)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue