From e40064120464719b592a9abe90471cc8a7201a87 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 13 Nov 2018 20:31:53 +0100 Subject: [PATCH 1/3] Fix Dispatcher bad behaviours in case of multiple Clients running at the same time. --- pyrogram/client/client.py | 8 ++++---- pyrogram/client/dispatcher/dispatcher.py | 14 ++++++-------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 8cb19c47..119ecc11 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -851,7 +851,7 @@ class Client(Methods, BaseClient): if len(self.channels_pts[channel_id]) > 50: 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)): diff = self.send( functions.updates.GetDifference( @@ -862,7 +862,7 @@ class Client(Methods, BaseClient): ) if diff.new_messages: - self.dispatcher.updates.put(( + self.dispatcher.updates_queue.put(( types.UpdateNewMessage( message=diff.new_messages[0], pts=updates.pts, @@ -872,9 +872,9 @@ class Client(Methods, BaseClient): diff.chats )) else: - self.dispatcher.updates.put((diff.other_updates[0], [], [])) + self.dispatcher.updates_queue.put((diff.other_updates[0], [], [])) elif isinstance(updates, types.UpdateShort): - self.dispatcher.updates.put((updates.update, [], [])) + self.dispatcher.updates_queue.put((updates.update, [], [])) elif isinstance(updates, types.UpdatesTooLong): log.warning(updates) except Exception as e: diff --git a/pyrogram/client/dispatcher/dispatcher.py b/pyrogram/client/dispatcher/dispatcher.py index fa65f987..a61e8077 100644 --- a/pyrogram/client/dispatcher/dispatcher.py +++ b/pyrogram/client/dispatcher/dispatcher.py @@ -52,17 +52,15 @@ class Dispatcher: MESSAGE_UPDATES = NEW_MESSAGE_UPDATES + EDIT_MESSAGE_UPDATES - UPDATES = None - def __init__(self, client, workers: int): self.client = client self.workers = workers self.workers_list = [] - self.updates = Queue() + self.updates_queue = Queue() self.groups = OrderedDict() - Dispatcher.UPDATES = { + self.update_parsers = { Dispatcher.MESSAGE_UPDATES: 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) } - 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): for i in range(self.workers): @@ -91,7 +89,7 @@ class Dispatcher: def stop(self): for _ in range(self.workers): - self.updates.put(None) + self.updates_queue.put(None) for worker in self.workers_list: worker.join() @@ -116,7 +114,7 @@ class Dispatcher: log.debug("{} started".format(name)) while True: - update = self.updates.get() + update = self.updates_queue.get() if update is None: break @@ -126,7 +124,7 @@ class Dispatcher: chats = {i.id: i for i in update[2]} update = update[0] - parser = Dispatcher.UPDATES.get(type(update), None) + parser = self.update_parsers.get(type(update), None) if parser is None: continue From e9ff43d71f348dbba468eb2c507b8276ee2c2c34 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 13 Nov 2018 20:33:55 +0100 Subject: [PATCH 2/3] Update reported version on docs --- docs/source/start/Installation.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/start/Installation.rst b/docs/source/start/Installation.rst index 7886f1d4..efebac5a 100644 --- a/docs/source/start/Installation.rst +++ b/docs/source/start/Installation.rst @@ -82,7 +82,7 @@ If no error shows up you are good to go. >>> import pyrogram >>> pyrogram.__version__ - '0.9.2' + '0.9.3' .. _TgCrypto: https://docs.pyrogram.ml/resources/TgCrypto .. _develop: http://github.com/pyrogram/pyrogram From 54e753986573f60e3706c1a158ff9b2050caa84f Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 13 Nov 2018 20:34:33 +0100 Subject: [PATCH 3/3] Update to v0.9.3 --- pyrogram/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/__init__.py b/pyrogram/__init__.py index 8471dd82..b1a5430c 100644 --- a/pyrogram/__init__.py +++ b/pyrogram/__init__.py @@ -23,7 +23,7 @@ __copyright__ = "Copyright (C) 2017-2018 Dan Tès