2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Remove handlers lock

This commit is contained in:
Dan 2018-05-26 18:04:17 +02:00
parent b04bce9dba
commit 93218a6ed7

View File

@ -50,8 +50,6 @@ class Dispatcher:
self.updates = Queue() self.updates = Queue()
self.groups = OrderedDict() self.groups = OrderedDict()
self._handler_lock = threading.Lock()
def start(self): def start(self):
for i in range(self.workers): for i in range(self.workers):
self.workers_list.append( self.workers_list.append(
@ -73,52 +71,49 @@ class Dispatcher:
self.workers_list.clear() self.workers_list.clear()
def add_handler(self, handler, group: int): def add_handler(self, handler, group: int):
with self._handler_lock: if group not in self.groups:
if group not in self.groups: self.groups[group] = []
self.groups[group] = [] self.groups = OrderedDict(sorted(self.groups.items()))
self.groups = OrderedDict(sorted(self.groups.items()))
self.groups[group].append(handler) self.groups[group].append(handler)
def remove_handler(self, handler, group: int): def remove_handler(self, handler, group: int):
with self._handler_lock: if group not in self.groups:
if group not in self.groups: raise ValueError("Group {} does not exist. "
raise ValueError("Group {} does not exist. " "Handler was not removed.".format(group))
"Handler was not removed.".format(group)) self.groups[group].remove(handler)
self.groups[group].remove(handler)
def dispatch(self, update, users: dict = None, chats: dict = None, is_raw: bool = False): def dispatch(self, update, users: dict = None, chats: dict = None, is_raw: bool = False):
with self._handler_lock: for group in self.groups.values():
for group in self.groups.values(): for handler in group:
for handler in group: if is_raw:
if is_raw: if not isinstance(handler, RawUpdateHandler):
if not isinstance(handler, RawUpdateHandler): continue
args = (self.client, update, users, chats)
else:
message = (update.message
or update.channel_post
or update.edited_message
or update.edited_channel_post)
callback_query = update.callback_query
if message and isinstance(handler, MessageHandler):
if not handler.check(message):
continue continue
args = (self.client, update, users, chats) args = (self.client, message)
elif callback_query and isinstance(handler, CallbackQueryHandler):
if not handler.check(callback_query):
continue
args = (self.client, callback_query)
else: else:
message = (update.message continue
or update.channel_post
or update.edited_message
or update.edited_channel_post)
callback_query = update.callback_query handler.callback(*args)
break
if message and isinstance(handler, MessageHandler):
if not handler.check(message):
continue
args = (self.client, message)
elif callback_query and isinstance(handler, CallbackQueryHandler):
if not handler.check(callback_query):
continue
args = (self.client, callback_query)
else:
continue
handler.callback(*args)
break
def update_worker(self): def update_worker(self):
name = threading.current_thread().name name = threading.current_thread().name