Rework dispatcher for asyncio
This commit is contained in:
parent
301ba799cf
commit
7bc4490680
@ -60,18 +60,23 @@ class Dispatcher:
|
|||||||
self.updates = asyncio.Queue()
|
self.updates = asyncio.Queue()
|
||||||
self.groups = OrderedDict()
|
self.groups = OrderedDict()
|
||||||
|
|
||||||
|
async def message_parser(update, users, chats):
|
||||||
|
return await utils.parse_messages(self.client, update.message, users, chats), MessageHandler
|
||||||
|
|
||||||
|
async def deleted_messages_parser(update, users, chats):
|
||||||
|
return utils.parse_deleted_messages(update), DeletedMessagesHandler
|
||||||
|
|
||||||
|
async def callback_query_parser(update, users, chats):
|
||||||
|
return await utils.parse_callback_query(self.client, update, users), CallbackQueryHandler
|
||||||
|
|
||||||
|
async def user_status_parser(update, users, chats):
|
||||||
|
return utils.parse_user_status(update.status, update.user_id), UserStatusHandler
|
||||||
|
|
||||||
Dispatcher.UPDATES = {
|
Dispatcher.UPDATES = {
|
||||||
Dispatcher.MESSAGE_UPDATES:
|
Dispatcher.MESSAGE_UPDATES: message_parser,
|
||||||
lambda upd, usr, cht: (utils.parse_messages(self.client, upd.message, usr, cht), MessageHandler),
|
Dispatcher.DELETE_MESSAGE_UPDATES: deleted_messages_parser,
|
||||||
|
Dispatcher.CALLBACK_QUERY_UPDATES: callback_query_parser,
|
||||||
Dispatcher.DELETE_MESSAGE_UPDATES:
|
(types.UpdateUserStatus,): user_status_parser
|
||||||
lambda upd, usr, cht: (utils.parse_deleted_messages(upd), DeletedMessagesHandler),
|
|
||||||
|
|
||||||
Dispatcher.CALLBACK_QUERY_UPDATES:
|
|
||||||
lambda upd, usr, cht: (utils.parse_callback_query(self.client, upd, usr), CallbackQueryHandler),
|
|
||||||
|
|
||||||
(types.UpdateUserStatus,):
|
|
||||||
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}
|
Dispatcher.UPDATES = {key: value for key_tuple, value in Dispatcher.UPDATES.items() for key in key_tuple}
|
||||||
@ -125,8 +130,7 @@ class Dispatcher:
|
|||||||
if parser is None:
|
if parser is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
update, handler_type = parser(update, users, chats)
|
update, handler_type = await parser(update, users, chats)
|
||||||
tasks = []
|
|
||||||
|
|
||||||
for group in self.groups.values():
|
for group in self.groups.values():
|
||||||
for handler in group:
|
for handler in group:
|
||||||
@ -142,12 +146,10 @@ class Dispatcher:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tasks.append(handler.callback(self.client, *args))
|
await handler.callback(self.client, *args)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(e, exc_info=True)
|
log.error(e, exc_info=True)
|
||||||
finally:
|
finally:
|
||||||
break
|
break
|
||||||
|
|
||||||
await asyncio.gather(*tasks)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(e, exc_info=True)
|
log.error(e, exc_info=True)
|
||||||
|
Loading…
Reference in New Issue
Block a user