From 0479c24e5a3e5651207b511f3c4d5fea93abd43f Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Sun, 15 Apr 2018 06:32:44 +0300 Subject: [PATCH] Optimize registering of callbacks --- aiogram/utils/executor.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/aiogram/utils/executor.py b/aiogram/utils/executor.py index b375f135..26d2eb75 100644 --- a/aiogram/utils/executor.py +++ b/aiogram/utils/executor.py @@ -14,21 +14,10 @@ APP_EXECUTOR_KEY = 'APP_EXECUTOR' def _setup_callbacks(executor, on_startup, on_shutdown): - if on_startup is None: - pass - elif callable(on_startup): + if on_startup is not None: executor.on_startup(on_startup) - else: - for callback in on_startup: - executor.on_startup(callback) - - if on_shutdown is None: - pass - elif callable(on_shutdown): + if on_shutdown is not None: executor.on_shutdown(on_shutdown) - else: - for callback in on_shutdown: - executor.on_shutdown(callback) def start_polling(dispatcher, *, loop=None, skip_updates=False, reset_webhook=True, @@ -83,11 +72,15 @@ class Executor: def on_startup(self, callback: callable, polling=True, webhook=True): self._check_frozen() - if not webhook and not polling: warn('This action has no effect!', UserWarning) return + if isinstance(callback, (list, tuple, set)): + for cb in callback: + self.on_startup(cb, polling, webhook) + return + if polling: self._on_startup_polling.append(callback) if webhook: @@ -95,11 +88,15 @@ class Executor: def on_shutdown(self, callback: callable, polling=True, webhook=True): self._check_frozen() - if not webhook and not polling: warn('This action has no effect!', UserWarning) return + if isinstance(callback, (list, tuple, set)): + for cb in callback: + self.on_shutdown(cb, polling, webhook) + return + if polling: self._on_shutdown_polling.append(callback) if webhook: