Small optimization and stop using bare-except.

This commit is contained in:
Alex Root Junior 2017-08-16 19:34:14 +03:00
parent be28a92aba
commit d4f1b72d92

View file

@ -2,6 +2,7 @@ import asyncio
import logging import logging
import typing import typing
from aiogram.utils.exceptions import TelegramAPIError, NetworkError
from .filters import CommandsFilter, RegexpFilter, ContentTypeFilter, generate_default_filters from .filters import CommandsFilter, RegexpFilter, ContentTypeFilter, generate_default_filters
from .handler import Handler from .handler import Handler
from .storage import DisabledStorage, BaseStorage, FSMContext from .storage import DisabledStorage, BaseStorage, FSMContext
@ -77,7 +78,7 @@ class Dispatcher:
""" """
tasks = [] tasks = []
for update in updates: for update in updates:
tasks.append(self.loop.create_task(self.updates_handler.notify(update))) tasks.append(self.updates_handler.notify(update))
return await asyncio.gather(*tasks) return await asyncio.gather(*tasks)
async def process_update(self, update): async def process_update(self, update):
@ -125,10 +126,9 @@ class Dispatcher:
while self._pooling: while self._pooling:
try: try:
updates = await self.bot.get_updates(limit=limit, offset=offset, timeout=timeout) updates = await self.bot.get_updates(limit=limit, offset=offset, timeout=timeout)
except Exception as e: except NetworkError:
log.exception('Cause exception while getting updates') log.exception('Cause exception while getting updates.')
if relax: await asyncio.sleep(15)
await asyncio.sleep(relax)
continue continue
if updates: if updates:
@ -137,7 +137,8 @@ class Dispatcher:
self.loop.create_task(self._process_pooling_updates(updates)) self.loop.create_task(self._process_pooling_updates(updates))
await asyncio.sleep(relax) if relax:
await asyncio.sleep(relax)
log.warning('Pooling is stopped.') log.warning('Pooling is stopped.')
@ -157,7 +158,7 @@ class Dispatcher:
if need_to_call: if need_to_call:
try: try:
asyncio.gather(*need_to_call) asyncio.gather(*need_to_call)
except Exception as e: except TelegramAPIError:
log.exception('Cause exception while processing updates.') log.exception('Cause exception while processing updates.')
def stop_pooling(self): def stop_pooling(self):