From 8d0e161b5602775dc4697ac44b264c81a58c70b9 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 21 Jun 2019 01:53:17 +0200 Subject: [PATCH 1/2] Lock dispatcher groups. Fixes #255 --- pyrogram/client/ext/dispatcher.py | 59 +++++++++++++++++-------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/pyrogram/client/ext/dispatcher.py b/pyrogram/client/ext/dispatcher.py index 56cdead6..2224bda9 100644 --- a/pyrogram/client/ext/dispatcher.py +++ b/pyrogram/client/ext/dispatcher.py @@ -20,7 +20,7 @@ import logging import threading from collections import OrderedDict from queue import Queue -from threading import Thread +from threading import Thread, Lock import pyrogram from pyrogram.api import types @@ -64,6 +64,8 @@ class Dispatcher: self.updates_queue = Queue() self.groups = OrderedDict() + self.lock = Lock() + self.update_parsers = { Dispatcher.MESSAGE_UPDATES: lambda upd, usr, cht: (pyrogram.Message._parse(self.client, upd.message, usr, cht), MessageHandler), @@ -110,17 +112,19 @@ class Dispatcher: self.groups.clear() def add_handler(self, handler, group: int): - if group not in self.groups: - self.groups[group] = [] - self.groups = OrderedDict(sorted(self.groups.items())) + with self.lock: + if group not in self.groups: + self.groups[group] = [] + self.groups = OrderedDict(sorted(self.groups.items())) - self.groups[group].append(handler) + self.groups[group].append(handler) def remove_handler(self, handler, group: int): - if group not in self.groups: - raise ValueError("Group {} does not exist. Handler was not removed.".format(group)) + with self.lock: + if group not in self.groups: + raise ValueError("Group {} does not exist. Handler was not removed.".format(group)) - self.groups[group].remove(handler) + self.groups[group].remove(handler) def update_worker(self): name = threading.current_thread().name @@ -142,29 +146,30 @@ class Dispatcher: else (None, type(None)) ) - for group in self.groups.values(): - for handler in group: - args = None + with self.lock: + for group in self.groups.values(): + for handler in group: + args = None - if isinstance(handler, handler_type): - if handler.check(parsed_update): - args = (parsed_update,) - elif isinstance(handler, RawUpdateHandler): - args = (update, users, chats) + if isinstance(handler, handler_type): + if handler.check(parsed_update): + args = (parsed_update,) + elif isinstance(handler, RawUpdateHandler): + args = (update, users, chats) - if args is None: - continue + if args is None: + continue - try: - handler.callback(self.client, *args) - except pyrogram.StopPropagation: - raise - except pyrogram.ContinuePropagation: - continue - except Exception as e: - log.error(e, exc_info=True) + try: + handler.callback(self.client, *args) + except pyrogram.StopPropagation: + raise + except pyrogram.ContinuePropagation: + continue + except Exception as e: + log.error(e, exc_info=True) - break + break except pyrogram.StopPropagation: pass except Exception as e: From a398bc5fc72c050ff90d8f82015755b2eccad3bd Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 21 Jun 2019 02:00:29 +0200 Subject: [PATCH 2/2] Rename CallbackQuery's bound-methods: edit_* -> edit_message_* --- docs/source/api/bound-methods.rst | 18 +++++++++--------- .../types/bots_and_keyboards/callback_query.py | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/source/api/bound-methods.rst b/docs/source/api/bound-methods.rst index e6729da6..83b3dbbe 100644 --- a/docs/source/api/bound-methods.rst +++ b/docs/source/api/bound-methods.rst @@ -81,13 +81,13 @@ CallbackQuery ^^^^^^^^^^^^^ .. hlist:: - :columns: 4 + :columns: 3 - :meth:`~CallbackQuery.answer` - - :meth:`~CallbackQuery.edit_text` - - :meth:`~CallbackQuery.edit_caption` - - :meth:`~CallbackQuery.edit_media` - - :meth:`~CallbackQuery.edit_reply_markup` + - :meth:`~CallbackQuery.edit_message_text` + - :meth:`~CallbackQuery.edit_message_caption` + - :meth:`~CallbackQuery.edit_message_media` + - :meth:`~CallbackQuery.edit_message_reply_markup` InlineQuery ^^^^^^^^^^^ @@ -141,10 +141,10 @@ Details .. CallbackQuery .. automethod:: CallbackQuery.answer() -.. automethod:: CallbackQuery.edit_text() -.. automethod:: CallbackQuery.edit_caption() -.. automethod:: CallbackQuery.edit_media() -.. automethod:: CallbackQuery.edit_reply_markup() +.. automethod:: CallbackQuery.edit_message_text() +.. automethod:: CallbackQuery.edit_message_caption() +.. automethod:: CallbackQuery.edit_message_media() +.. automethod:: CallbackQuery.edit_message_reply_markup() .. InlineQuery .. automethod:: InlineQuery.answer() diff --git a/pyrogram/client/types/bots_and_keyboards/callback_query.py b/pyrogram/client/types/bots_and_keyboards/callback_query.py index fcc90e57..6a717489 100644 --- a/pyrogram/client/types/bots_and_keyboards/callback_query.py +++ b/pyrogram/client/types/bots_and_keyboards/callback_query.py @@ -173,14 +173,14 @@ class CallbackQuery(Object, Update): cache_time=cache_time ) - def edit_text( + def edit_message_text( self, text: str, parse_mode: str = "", disable_web_page_preview: bool = None, reply_markup: "pyrogram.InlineKeyboardMarkup" = None ) -> Union["pyrogram.Message", bool]: - """Edit the text of messages attached to this callback query. + """Edit the text of messages attached to callback queries. Bound method *edit_message_text* of :obj:`CallbackQuery`. @@ -223,13 +223,13 @@ class CallbackQuery(Object, Update): reply_markup=reply_markup ) - def edit_caption( + def edit_message_caption( self, caption: str, parse_mode: str = "", reply_markup: "pyrogram.InlineKeyboardMarkup" = None ) -> Union["pyrogram.Message", bool]: - """Edit the caption of media messages attached to this callback query. + """Edit the caption of media messages attached to callback queries. Bound method *edit_message_caption* of :obj:`CallbackQuery`. @@ -251,14 +251,14 @@ class CallbackQuery(Object, Update): Raises: RPCError: In case of a Telegram RPC error. """ - return self.edit_text(caption, parse_mode, reply_markup) + return self.edit_message_text(caption, parse_mode, reply_markup) - def edit_media( + def edit_message_media( self, media: "pyrogram.InputMedia", reply_markup: "pyrogram.InlineKeyboardMarkup" = None ) -> Union["pyrogram.Message", bool]: - """Edit animation, audio, document, photo or video messages attached to this callback query. + """Edit animation, audio, document, photo or video messages attached to callback queries. Bound method *edit_message_media* of :obj:`CallbackQuery`. @@ -290,11 +290,11 @@ class CallbackQuery(Object, Update): reply_markup=reply_markup ) - def edit_reply_markup( + def edit_message_reply_markup( self, reply_markup: "pyrogram.InlineKeyboardMarkup" = None ) -> Union["pyrogram.Message", bool]: - """Edit only the reply markup of messages attached to this callback query. + """Edit only the reply markup of messages attached to callback queries. Bound method *edit_message_reply_markup* of :obj:`CallbackQuery`.