Fix Dispatcher bad behaviours in case of multiple Clients running at the same time.

This commit is contained in:
Dan 2018-11-13 20:31:53 +01:00
parent 5b10afa7a3
commit e400641204
2 changed files with 10 additions and 12 deletions

View File

@ -851,7 +851,7 @@ class Client(Methods, BaseClient):
if len(self.channels_pts[channel_id]) > 50: if len(self.channels_pts[channel_id]) > 50:
self.channels_pts[channel_id] = self.channels_pts[channel_id][25:] self.channels_pts[channel_id] = self.channels_pts[channel_id][25:]
self.dispatcher.updates.put((update, updates.users, updates.chats)) self.dispatcher.updates_queue.put((update, updates.users, updates.chats))
elif isinstance(updates, (types.UpdateShortMessage, types.UpdateShortChatMessage)): elif isinstance(updates, (types.UpdateShortMessage, types.UpdateShortChatMessage)):
diff = self.send( diff = self.send(
functions.updates.GetDifference( functions.updates.GetDifference(
@ -862,7 +862,7 @@ class Client(Methods, BaseClient):
) )
if diff.new_messages: if diff.new_messages:
self.dispatcher.updates.put(( self.dispatcher.updates_queue.put((
types.UpdateNewMessage( types.UpdateNewMessage(
message=diff.new_messages[0], message=diff.new_messages[0],
pts=updates.pts, pts=updates.pts,
@ -872,9 +872,9 @@ class Client(Methods, BaseClient):
diff.chats diff.chats
)) ))
else: else:
self.dispatcher.updates.put((diff.other_updates[0], [], [])) self.dispatcher.updates_queue.put((diff.other_updates[0], [], []))
elif isinstance(updates, types.UpdateShort): elif isinstance(updates, types.UpdateShort):
self.dispatcher.updates.put((updates.update, [], [])) self.dispatcher.updates_queue.put((updates.update, [], []))
elif isinstance(updates, types.UpdatesTooLong): elif isinstance(updates, types.UpdatesTooLong):
log.warning(updates) log.warning(updates)
except Exception as e: except Exception as e:

View File

@ -52,17 +52,15 @@ class Dispatcher:
MESSAGE_UPDATES = NEW_MESSAGE_UPDATES + EDIT_MESSAGE_UPDATES MESSAGE_UPDATES = NEW_MESSAGE_UPDATES + EDIT_MESSAGE_UPDATES
UPDATES = None
def __init__(self, client, workers: int): def __init__(self, client, workers: int):
self.client = client self.client = client
self.workers = workers self.workers = workers
self.workers_list = [] self.workers_list = []
self.updates = Queue() self.updates_queue = Queue()
self.groups = OrderedDict() self.groups = OrderedDict()
Dispatcher.UPDATES = { self.update_parsers = {
Dispatcher.MESSAGE_UPDATES: Dispatcher.MESSAGE_UPDATES:
lambda upd, usr, cht: (utils.parse_messages(self.client, upd.message, usr, cht), MessageHandler), lambda upd, usr, cht: (utils.parse_messages(self.client, upd.message, usr, cht), MessageHandler),
@ -76,7 +74,7 @@ class Dispatcher:
lambda upd, usr, cht: (utils.parse_user_status(upd.status, upd.user_id), UserStatusHandler) lambda upd, usr, cht: (utils.parse_user_status(upd.status, upd.user_id), UserStatusHandler)
} }
Dispatcher.UPDATES = {key: value for key_tuple, value in Dispatcher.UPDATES.items() for key in key_tuple} self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple}
def start(self): def start(self):
for i in range(self.workers): for i in range(self.workers):
@ -91,7 +89,7 @@ class Dispatcher:
def stop(self): def stop(self):
for _ in range(self.workers): for _ in range(self.workers):
self.updates.put(None) self.updates_queue.put(None)
for worker in self.workers_list: for worker in self.workers_list:
worker.join() worker.join()
@ -116,7 +114,7 @@ class Dispatcher:
log.debug("{} started".format(name)) log.debug("{} started".format(name))
while True: while True:
update = self.updates.get() update = self.updates_queue.get()
if update is None: if update is None:
break break
@ -126,7 +124,7 @@ class Dispatcher:
chats = {i.id: i for i in update[2]} chats = {i.id: i for i in update[2]}
update = update[0] update = update[0]
parser = Dispatcher.UPDATES.get(type(update), None) parser = self.update_parsers.get(type(update), None)
if parser is None: if parser is None:
continue continue